List of usage examples for com.google.gwt.json.client JSONValue isArray
public JSONArray isArray()
From source file:org.gwt.dynamic.common.client.util.JsonHelper.java
License:MIT License
public static <T> List<T> fromJsonList(Class<T> beanClass, JSONValue json) { if (json == null) throw new NullPointerException(); return getReaderForBean(beanClass).readList(json.isArray()); }
From source file:org.gwt.json.serialization.client.collections.CollectionUtils.java
License:Apache License
public static <T> void jsonToCollection(JSONValue objects, JsonGenericTypeSerializer<T> objectSerializer, GenericType[] genericTypes, Collection<T> collection) { if (objects == null) return;//w w w . j a v a2 s . c om JSONArray array = objects.isArray(); if (array == null) return; for (int i = 0; i < array.size(); i++) { collection.add(objectSerializer.deSerialize(array.get(i), genericTypes)); } }
From source file:org.jax.gwtutil.client.JsonListDeserializer.java
License:Open Source License
/** * {@inheritDoc}/* ww w . ja v a2 s . c o m*/ */ public List<T> fromJsonValue(JSONValue jsonValue) throws IllegalArgumentException { JSONArray jsonArray = jsonValue.isArray(); if (jsonArray == null) { throw new IllegalArgumentException( "Cannot deserialize the given JSON value because it is " + "not an array"); } else { int jsonArraySize = jsonArray.size(); List<T> list = new ArrayList<T>(jsonArraySize); for (int i = 0; i < jsonArraySize; i++) { JSONValue currJsonVal = jsonArray.get(i); list.add(this.elementDeserializer.fromJsonValue(currJsonVal)); } return list; } }
From source file:org.jax.gwtutil.client.JsonUtil.java
License:Open Source License
/** * Convert the JSON string array into a Java string array * @param jsonArrayString/*from w w w. j a v a2 s . co m*/ * the JSON string array * @return * the Java strings */ public static String[] toStringArray(String jsonArrayString) { JSONValue jsonStrains = JSONParser.parse(jsonArrayString); JSONArray jsonStrainsArray = jsonStrains.isArray(); if (jsonStrainsArray != null) { final String[] stringArray = new String[jsonStrainsArray.size()]; for (int i = 0; i < stringArray.length; i++) { JSONString currString = jsonStrainsArray.get(i).isString(); if (currString != null) { stringArray[i] = currString.stringValue(); } else { return null; } } return stringArray; } else { return null; } }
From source file:org.jboss.bpm.console.client.model.DTOParser.java
License:Open Source License
public static List<String> parseStringArray(JSONValue jso) { List<String> result = new ArrayList<String>(); JSONArray jsonArray = jso.isArray(); if (null == jsonArray) throw new IllegalArgumentException("Not an array: " + jso); for (int i = 0; i < jsonArray.size(); i++) { JSONValue jsonValue = jsonArray.get(i); if (jsonValue.toString().equals("null")) { ConsoleLog.warn("FIXME JBPM-1828: Null value on string array:" + jsonArray.toString()); continue; // TODO: JBPM-1828 }/*www . j a v a 2 s. c o m*/ JSONString item = jsonValue.isString(); result.add(item.stringValue()); } return result; }
From source file:org.jboss.bpm.console.client.process.JSONTree.java
License:Open Source License
private void parseObject(TreeItem root, String key, JSONValue topLevel) { JSONObject rootJSO = topLevel.isObject(); if (null == rootJSO) throw new IllegalArgumentException("Not a JSON object: " + topLevel); for (String innerKey : rootJSO.keySet()) { JSONValue jsonValue = rootJSO.get(innerKey); if (jsonValue.isObject() != null) { parseObject(root, innerKey, jsonValue); } else if (jsonValue.isArray() != null) { parseArray(root, innerKey, jsonValue); } else {/*from w ww. ja v a2s . c o m*/ parseValue(root, innerKey, jsonValue); } } }
From source file:org.jboss.errai.bus.client.json.JSONUtilCli.java
License:Apache License
public static ArrayList<MarshalledMessage> decodePayload(String value) { if (value == null || value.trim().length() == 0) return EMPTYLIST; /**//w w w .j a v a 2 s . co m * We have to do a two-stage decoding of the message. We cannot fully decode the message here, as we * cannot be sure the destination endpoint exists within this Errai bundle. So we extract the ToSubject * field and send the unparsed JSON object onwards. * */ try { JSONValue val = JSONParser.parseStrict(value); if (val == null) { return EMPTYLIST; } JSONArray arr = val.isArray(); if (arr == null) { throw new RuntimeException("unrecognized payload" + val.toString()); } ArrayList<MarshalledMessage> list = new ArrayList<MarshalledMessage>(arr.size()); for (int i = 0; i < arr.size(); i++) { list.add(new MarshalledMessageImpl((JSONObject) arr.get(i))); } return list; } catch (Exception e) { System.out.println("JSONUtilCli.decodePayload=" + value); e.printStackTrace(); return EMPTYLIST; } }
From source file:org.jboss.errai.bus.client.util.BusToolsCli.java
License:Apache License
public static List<Message> decodePayload(final String jsonString) { if (jsonString == null || jsonString.trim().length() == 0) return Collections.emptyList(); final JSONValue val = JSONParser.parseStrict(jsonString); if (val == null || val.isArray() == null) { throw new RuntimeException("illegal payload: must be JSONArray"); }/*from w w w .j a va 2 s . c o m*/ final JSONArray jsonArray = val.isArray(); final List<Message> messageList = new ArrayList<Message>(jsonArray.size()); for (int i = 0; i < jsonArray.size(); i++) { messageList.add(decodeCommandMessage(GWTJSON.wrap(jsonArray.get(i)))); } return messageList; }
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 {/*w ww . ja va 2s .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;// w w w . jav a 2s . 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; }