List of usage examples for com.google.gwt.json.client JSONValue isNumber
public JSONNumber isNumber()
From source file:org.gwt.json.serialization.client.primitives.JsonFloatSerializer.java
License:Apache License
@Override public Float deSerialize(JSONValue jsonObj, boolean forcePrimitiveValue) throws JSONException { Float defaultValue = forcePrimitiveValue ? .0f : null; return jsonObj == null ? defaultValue : (jsonObj.isNumber() == null ? defaultValue : (float) jsonObj.isNumber().doubleValue()); }
From source file:org.gwt.json.serialization.client.primitives.JsonIntegerSerializer.java
License:Apache License
@Override public Integer deSerialize(JSONValue jsonObj, boolean forcePrimitiveValue) throws JSONException { Integer defaultValue = forcePrimitiveValue ? 0 : null; return jsonObj == null ? defaultValue : (jsonObj.isNumber() == null ? defaultValue : (int) jsonObj.isNumber().doubleValue()); }
From source file:org.gwt.json.serialization.client.primitives.JsonLongSerializer.java
License:Apache License
@Override public Long deSerialize(JSONValue jsonObj, boolean forcePrimitiveValue) throws JSONException { Long defaultValue = forcePrimitiveValue ? 0L : null; return jsonObj == null ? defaultValue : (jsonObj.isNumber() == null ? defaultValue : (long) jsonObj.isNumber().doubleValue()); }
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 .co 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 {/*from www .ja va 2 s . c o m*/ throw new RuntimeException("unknown encoding"); } }
From source file:org.jboss.errai.common.client.types.JSONTypeHelper.java
License:Apache License
public static Object convert(JSONValue value, Class to, DecodingContext ctx) { JSONValue v;/*from w w w . jav a 2 s .c o m*/ if ((v = value.isString()) != null) { return TypeHandlerFactory.convert(String.class, to, ((JSONString) v).stringValue(), ctx); } else if ((v = value.isNumber()) != null) { return TypeHandlerFactory.convert(Number.class, to, ((JSONNumber) v).doubleValue(), ctx); } else if ((v = value.isBoolean()) != null) { return TypeHandlerFactory.convert(Boolean.class, to, ((JSONBoolean) v).booleanValue(), ctx); } else if ((v = value.isArray()) != null) { List list = new ArrayList(); JSONArray arr = (JSONArray) v; Class cType = to.getComponentType(); while (cType != null && cType.getComponentType() != null) cType = cType.getComponentType(); if (cType == null) cType = to; Object o; for (int i = 0; i < arr.size(); i++) { if ((o = convert(arr.get(i), cType, ctx)) instanceof UnsatisfiedForwardLookup) { ctx.addUnsatisfiedDependency(list, (UnsatisfiedForwardLookup) o); } else { list.add(convert(arr.get(i), cType, ctx)); } } Object t = TypeHandlerFactory.convert(Collection.class, to, list, ctx); ctx.swapDepReference(list, t); return t; } else if ((v = value.isObject()) != null) { JSONObject eMap = (JSONObject) v; Map<Object, Object> m = new UHashMap<Object, Object>(); Object o; Object val; for (String key : eMap.keySet()) { o = key; if (key.startsWith(SerializationParts.EMBEDDED_JSON)) { o = JSONDecoderCli.decode(key.substring(SerializationParts.EMBEDDED_JSON.length()), ctx); } else if (SerializationParts.ENCODED_TYPE.equals(key)) { String className = eMap.get(key).isString().stringValue(); String objId = null; if ((v = eMap.get(SerializationParts.OBJECT_ID)) != null) { objId = v.isString().stringValue(); } boolean ref = false; if (objId != null) { if (objId.charAt(0) == '$') { ref = true; objId = objId.substring(1); } if (ctx.hasObject(objId)) { return ctx.getObject(objId); } else if (ref) { return new UnsatisfiedForwardLookup(objId); } } if (TypeDemarshallers.hasDemarshaller(className)) { o = TypeDemarshallers.getDemarshaller(className).demarshall(eMap, ctx); if (objId != null) ctx.putObject(objId, o); return o; } else { throw new RuntimeException("no available demarshaller: " + className); } } val = JSONDecoderCli.decode(eMap.get(key), ctx); boolean commit = true; if (o instanceof UnsatisfiedForwardLookup) { ctx.addUnsatisfiedDependency(m, (UnsatisfiedForwardLookup) o); if (!(val instanceof UnsatisfiedForwardLookup)) { ((UnsatisfiedForwardLookup) o).setVal(val); } commit = false; } if (val instanceof UnsatisfiedForwardLookup) { ((UnsatisfiedForwardLookup) val).setKey(o); ctx.addUnsatisfiedDependency(m, (UnsatisfiedForwardLookup) val); commit = false; } if (commit) { m.put(o, JSONDecoderCli.decode(eMap.get(key), ctx)); } } return TypeHandlerFactory.convert(Map.class, to, m, ctx); } return null; }
From source file:org.jboss.errai.enterprise.client.jaxrs.JacksonTransformer.java
License:Apache License
/** * The transformation from Jackson's JSON to Errai JSON contains the following steps: * <ul>// w ww .ja va 2 s. com * <li>Recursively add an incremented OBJECT_ID to every JSON object</li> * <li>If a number is encountered, wrap it in a new JSON object with an OBJECT_ID and * NUMERIC_VALUE property</li> * <li>If an array is encountered, wrap it in a new JSON object with an OBJECT_ID and * QUALIFIED_VALUE property</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 objectId * last used object id * @return modified JSON value */ private static JSONValue fromJackson(JSONValue val, String key, JSONObject parent, int[] objectId) { JSONObject obj; JSONNumber num; JSONArray arr; if ((obj = val.isObject()) != null) { obj.put(OBJECT_ID, new JSONString(new Integer(++objectId[0]).toString())); for (String k : obj.keySet()) { fromJackson(obj.get(k), k, obj, objectId); } } else if ((num = val.isNumber()) != null) { JSONObject numObject = new JSONObject(); numObject.put(OBJECT_ID, new JSONString(new Integer(++objectId[0]).toString())); numObject.put(NUMERIC_VALUE, num); if (parent != null) { parent.put(key, numObject); } else { val = numObject; } } else if ((arr = val.isArray()) != null) { JSONObject arrayObject = new JSONObject(); arrayObject.put(OBJECT_ID, new JSONString(new Integer(++objectId[0]).toString())); arrayObject.put(QUALIFIED_VALUE, arr); if (parent != null) { parent.put(key, arrayObject); } else { val = arrayObject; } for (int i = 0; i < arr.size(); i++) { arr.set(i, fromJackson(arr.get(i), QUALIFIED_VALUE, null, objectId)); } } return val; }
From source file:org.jbpm.formapi.client.form.FormRepresentationDecoderClient.java
License:Apache License
private Object fromJsonValue(JSONValue elem) { if (elem.isString() != null) { return elem.isString().stringValue(); } else if (elem.isNumber() != null) { return elem.isNumber().doubleValue(); } else if (elem.isArray() != null) { return asList(elem.isArray()); } else if (elem.isNull() != null) { return null; } else if (elem.isObject() != null) { return asMap(elem.isObject()); } else {/*from w ww .java2 s .c om*/ return ""; } }
From source file:org.jbpm.formbuilder.client.JsonLoadInput.java
License:Apache License
private static Object asActualValue(JSONValue value) { if (value.isArray() != null) { JSONArray arr = value.isArray(); List<Object> retval = new ArrayList<Object>(); for (int index = 0; index < arr.size(); index++) { JSONValue subValue = arr.get(index); retval.add(asActualValue(subValue)); }/* w w w . j a v a2s. com*/ return retval; } else if (value.isBoolean() != null) { return String.valueOf(value.isBoolean().booleanValue()); } else if (value.isNull() != null) { return null; } else if (value.isNumber() != null) { return String.valueOf(value.isNumber().doubleValue()); } else if (value.isString() != null) { return value.isString().stringValue(); } else if (value.isObject() != null) { return toFormData(value.isObject()); } return null; }
From source file:org.lirazs.gbackbone.client.core.data.Options.java
License:Apache License
public Options(JSONValue jsonObject) { JSONObject object = jsonObject.isObject(); if (object != null) { for (String s : object.keySet()) { JSONValue jsonValue = object.get(s); Object value = jsonValue; JSONNumber number = jsonValue.isNumber(); if (number != null) { if (number.toString().contains(".")) { value = number.doubleValue(); } else { value = (int) number.doubleValue(); }/* w w w . j a v a2s .c o m*/ } JSONBoolean jsonBoolean = jsonValue.isBoolean(); if (jsonBoolean != null) value = jsonBoolean.booleanValue(); JSONNull jsonNull = jsonValue.isNull(); if (jsonNull != null) value = null; JSONString jsonString = jsonValue.isString(); if (jsonString != null) value = jsonString.stringValue(); put(s, value); } } }