List of usage examples for com.google.gwt.json.client JSONValue isString
public JSONString isString()
From source file:com.google.gwt.sample.hello.client.HelloJsonp.java
private void decodeJson(JSONValue jv) { JSONArray jsonArray;//from w w w . j a v a 2s . co m JSONObject jsonObject; JSONString jsonString; if ((jsonArray = jv.isArray()) != null) { str.append(" [[ \n"); indent += 4; prefix(); for (int i = 0; i < jsonArray.size(); ++i) { str.append("[" + Integer.toString(i) + "]=("); decodeJson(jsonArray.get(i)); str.append(")\n"); prefix(); } str.append("\n"); indent -= 4; prefix(); str.append(" ]] \n"); prefix(); } else if ((jsonObject = jv.isObject()) != null) { Set<String> keys = jsonObject.keySet(); str.append(" {{ \n"); indent += 4; prefix(); for (String key : keys) { str.append("{" + key + "}=("); decodeJson(jsonObject.get(key)); str.append(")\n"); prefix(); } str.append("\n"); indent -= 4; prefix(); str.append(" }} \n"); prefix(); } else if ((jsonString = jv.isString()) != null) { // Use stringValue instead of toString() because we don't want escaping str.append("\"" + jsonString.stringValue() + "\"\n"); prefix(); } else { // JSONBoolean, JSONNumber, and JSONNull work well with toString(). str.append(jv.toString() + "\n"); prefix(); } }
From source file:com.google.gwt.sample.json.client.JSON.java
License:Apache License
private void addChildren(TreeItem treeItem, JSONValue jsonValue) { JSONArray jsonArray;//from w ww. jav a 2 s .com JSONObject jsonObject; JSONString jsonString; if ((jsonArray = jsonValue.isArray()) != null) { for (int i = 0; i < jsonArray.size(); ++i) { TreeItem child = treeItem.addItem(getChildText("[" + Integer.toString(i) + "]")); addChildren(child, jsonArray.get(i)); } } else if ((jsonObject = jsonValue.isObject()) != null) { Set<String> keys = jsonObject.keySet(); for (String key : keys) { TreeItem child = treeItem.addItem(getChildText(key)); addChildren(child, jsonObject.get(key)); } } else if ((jsonString = jsonValue.isString()) != null) { // Use stringValue instead of toString() because we don't want escaping treeItem.addItem(jsonString.stringValue()); } else { // JSONBoolean, JSONNumber, and JSONNull work well with toString(). treeItem.addItem(getChildText(jsonValue.toString())); } }
From source file:com.googlecode.gwtphonegap.client.contacts.browser.ContactBrowserImpl.java
License:Apache License
private static String getFieldAsString(JSONValue field) { return (field != null && field.isString() != null) ? field.isString().stringValue() : ""; }
From source file:com.guit.client.jsorm.StringSerializer.java
License:Apache License
@Override protected String deserializeJson(JSONValue o) { return o.isString().stringValue(); }
From source file:com.hunchee.haystack.client.utils.JsonConverter.java
License:Open Source License
/** * Decodes a JSONObject to a map./*from w w w . j a va 2 s .c o m*/ * * @param jso the JSONObject * @return the map */ public static Map<String, Object> decode(JSONObject jso) { Map<String, Object> map = new LinkedHashMap<String, Object>(); for (String key : jso.keySet()) { JSONValue j = jso.get(key); if (j.isObject() != null) { map.put(key, decode(j.isObject())); } else if (j.isArray() != null) { map.put(key, decodeToList(j.isArray())); } else if (j.isBoolean() != null) { map.put(key, j.isBoolean().booleanValue()); } else if (j.isNumber() != null) { map.put(key, j.isNumber().doubleValue()); } else if (j.isString() != null) { //map.put(key, decodeValue(j.isString().stringValue())); map.put(key, j.isString().stringValue()); } } return map; }
From source file:com.hunchee.haystack.client.utils.JsonConverter.java
License:Open Source License
protected static List<Object> decodeToList(JSONArray array) { List<Object> list = new ArrayList<Object>(); for (int i = 0; i < array.size(); i++) { JSONValue v = array.get(i); if (v.isObject() != null) { list.add(decode(v.isObject())); } else if (v.isArray() != null) { list.add(decodeToList(v.isArray())); } else if (v.isNull() != null) { list.add(null);//www. j av a2s. c om } else if (v.isNumber() != null) { list.add(v.isNumber().doubleValue()); } else if (v.isBoolean() != null) { list.add(v.isBoolean().booleanValue()); } else if (v.isString() != null) { // list.add(decodeValue(v.isString().stringValue())); list.add(v.isString().stringValue()); } } return list; }
From source file:com.itgp.gwtviz.client.ui.runtime.RuntimeDataV1.java
License:Open Source License
protected void initColumnTypeFromJava(JSONArray parsedJSONData) { int numberOfRows = parsedJSONData.size(); int numberOfColumns = this.dataModel.getColMetaArray().length; for (int colModelIndex = 0; colModelIndex < numberOfColumns; colModelIndex++) { int testDataRows = 0; ColMeta colMeta = this.dataModel.getColMetaArray()[colModelIndex]; int colPos = colMeta.getColPos(); for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { JSONValue cell = ((JSONArray) parsedJSONData.get(rowIndex)).get(colPos); JSONString cellAsString = cell.isString(); if (cellAsString == null) { ServerComm.alert("row " + rowIndex + " col " + colPos + " : [" + cell.toString() + "] is not a String! This circumstance contradicts the design parameters given!"); return; }/*from ww w . j a v a2 s .c o m*/ String val = cellAsString.stringValue(); if (val != null && val.length() > 0) { testDataRows += 1; if ((colMeta.getType() == TYPE.STRING)) { break; // get out, no need to do more - a string cannot become a number again } else { // test for numeric in the first 3 rows if it's not already pegged as string try { double d = Double.parseDouble(val); colMeta.setType(TYPE.NUMBER); } catch (Exception ex) { colMeta.setType(TYPE.STRING); } } if (testDataRows >= 3) { break; // get out of the for loop - we have enough data points } } // if (val != null && val.length() > 0) } // for i } // for j }
From source file:com.kk_electronic.kkportal.core.rpc.jsonformat.JsonString.java
License:Open Source License
@Override public String fromJson(JSONValue jsonValue, List<Class<?>> subtypes, FrameEncoder<JSONValue> encoder) throws UnableToDeserialize { if (jsonValue.isNull() != null) return null; JSONString jsonString = jsonValue.isString(); if (jsonString == null) throw new UnableToDeserialize("Expected json String"); return jsonString.stringValue(); }
From source file:com.kk_electronic.kkportal.core.rpc.SimpleEncoder.java
License:Open Source License
private static Object convert(JSONValue y, ObjectHelper objectHelper) { if (y.isArray() != null) return convert(y.isArray(), objectHelper); if (y.isObject() != null) return convert(y.isObject(), objectHelper); if (y.isBoolean() != null) return y.isBoolean().booleanValue(); if (y.isString() != null) return y.isString().stringValue(); if (y.isNumber() != null) return y.isNumber().doubleValue(); if (y.isNull() != null) return null; throw new RuntimeException("Could not convert"); }
From source file:com.kk_electronic.kkportal.core.rpc.SimpleEncoder.java
License:Open Source License
@SuppressWarnings("unchecked") private <T> T decodeFromJson(List<Class<?>> resultSubTypes, JSONValue jsonvalue) { if (jsonvalue == null || jsonvalue.isNull() != null) return null; Class<?> target = resultSubTypes.get(0); if (target == List.class) { List<T> list = new ArrayList<T>(); JSONArray ja = jsonvalue.isArray(); for (int i = 0, l = ja.size(); i < l; i++) { list.add((T) decodeFromJson(resultSubTypes.subList(1, resultSubTypes.size()), ja.get(i))); }/* w w w. ja va2 s . c o m*/ return (T) list; } if (target == Map.class) { if (resultSubTypes.get(1) == String.class) { Map<String, Object> map = new HashMap<String, Object>(); JSONObject jo = jsonvalue.isObject(); for (String key : jo.keySet()) { map.put(key, decodeFromJson(resultSubTypes.subList(2, resultSubTypes.size()), jo.get(key))); } return (T) map; } } if (target == Response.class) { JSONObject jo = jsonvalue.isObject(); return (T) new ResponseDTO(jo); } if (target == String.class) { return (T) jsonvalue.isString().stringValue(); } if (target == TabInfo.class) { return (T) new TabInfoDTO(jsonvalue.isObject()); } if (target == ModuleInfo.class) { return (T) new ModuleInfoDTO(jsonvalue.isObject()); } if (target == Object.class) { return (T) jsonvalue; } GWT.log("DECODING-Can't convert type " + target.getName()); return null; }