List of usage examples for com.google.gwt.json.client JSONValue isObject
public JSONObject isObject()
From source file:org.freemedsoftware.gwt.client.i18n.I18nUtil.java
License:Open Source License
/** * Load language definitions from the server. * /*from w w w.j ava 2s . c om*/ * @param localeName * @param callback */ public static void loadLocale(String localeName, final Command callback) { String loadFrom = RESOURCES_DIR + localeName + ".json"; loadedLocaleName = localeName; RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(loadFrom)); BUSY_LOADING = true; try { builder.sendRequest(null, new RequestCallback() { public void onError(Request request, Throwable exception) { BUSY_LOADING = false; JsonUtil.debug(exception.toString()); } public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { long beginTime = System.currentTimeMillis(); JSONValue o = JSONParser.parseStrict(response.getText()); if (o.isObject() != null) { JsonUtil.debug("Bad data presented."); BUSY_LOADING = false; } // Empty out *everything* we have now. locale.clear(); // Import everything for (String key : ((JSONObject) o).keySet()) { if (((JSONObject) o).get(key) != null) { locale.put(key, ((JSONString) ((JSONObject) o).get(key)).stringValue()); } } long endTime = System.currentTimeMillis(); JsonUtil.debug("Locale " + loadedLocaleName + " parsed and loaded in " + (endTime - beginTime) + "ms"); BUSY_LOADING = false; } else { BUSY_LOADING = false; JsonUtil.debug(response.getStatusText()); } if (callback != null) { callback.execute(); } } }); } catch (RequestException e) { BUSY_LOADING = false; JsonUtil.debug(e.toString()); if (callback != null) { callback.execute(); } } }
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(); 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()); }//from w w w .j a va 2s . c o m } 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.fusesource.restygwt.client.Method.java
License:Apache License
public <T extends JavaScriptObject> void send(final OverlayCallback<T> callback) { defaultAcceptType(Resource.CONTENT_TYPE_JSON); try {// w w w . jav a 2s.co m send(new AbstractRequestCallback<T>(this, callback) { protected T parseResult() throws Exception { try { JSONValue val = JSONParser.parse(response.getText()); if (val.isObject() != null) { return (T) val.isObject().getJavaScriptObject(); } else if (val.isArray() != null) { return (T) val.isArray().getJavaScriptObject(); } else { throw new ResponseFormatException("Response was NOT a JSON object"); } } catch (JSONException e) { throw new ResponseFormatException("Response was NOT a valid JSON document", e); } catch (IllegalArgumentException e) { throw new ResponseFormatException("Response was NOT a valid JSON document", e); } } }); } catch (Throwable e) { GWT.log("Received http error for: " + builder.getHTTPMethod() + " " + builder.getUrl(), e); callback.onFailure(this, e); } }
From source file:org.fusesource.restygwt.client.ObjectEncoderDecoder.java
License:Apache License
@Override public Object decode(JSONValue value) throws org.fusesource.restygwt.client.JsonEncoderDecoder.DecodingException { if (value instanceof JSONNumber) return ((JSONNumber) value).doubleValue(); else if (value instanceof JSONBoolean) return ((JSONBoolean) value).booleanValue(); else if (value instanceof JSONString) return ((JSONString) value).stringValue(); else if (value instanceof JSONArray) { JSONArray array = value.isArray(); List<Object> list = new ArrayList<Object>(array.size()); for (int ct = 0; ct < array.size(); ct++) list.add(decode(array.get(ct))); return list; } else if (value instanceof JSONObject) { JSONObject object = value.isObject(); Map<String, Object> map = new HashMap<String, Object>(); for (String key : object.keySet()) map.put(key, decode(object.get(key))); return map; } else//from w w w.j ava2 s . com 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;//from www .ja v a2s . co 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 child JSON object from a parent JSON object. * /*w w w .j ava 2 s. c om*/ * @param jsonObject The parent JSON object. * @param key The name of the child object. * @return Returns the child JSON object if it could be found, or null if the value was null. * @throws JSONException In case something went wrong while searching for the child. */ public static JSONObject getChild(JSONObject jsonObject, String key) throws JSONException { checkArguments(jsonObject, key); JSONValue value = jsonObject.get(key); if (value != null) { if (value.isObject() != null) { return value.isObject(); } else if (value.isNull() != null) { return null; } throw new JSONException("Child is not a JSONObject, but a: " + value.getClass()); } return null; }
From source file:org.geomajas.gwt2.client.service.JsonService.java
License:Open Source License
/** * Get a geometry value from a {@link JSONObject}. * //w w w. j a va 2 s . c o 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 or null. * @throws JSONException Thrown in case the key could not be found in the JSON object, or if the date could not be * parsed correctly. */ public static Geometry getGeometryValue(JSONObject jsonObject, String key) throws JSONException { checkArguments(jsonObject, key); JSONValue value = jsonObject.get(key); if (value != null && value.isObject() != null) { JSONObject obj = value.isObject(); String type = JsonService.getStringValue(obj, "type"); Geometry geometry = new Geometry(type, 0, 5); JSONArray array = JsonService.getChildArray(obj, "coordinates"); switch (GeometryType.fromValue(type)) { case POINT: parseSimple(GeometryType.POINT, geometry, array); break; case LINESTRING: parseSimple(GeometryType.LINEARRING, geometry, array); break; case MULTILINESTRING: parseCollection(GeometryType.LINESTRING, geometry, array); break; case MULTIPOINT: parseCollection(GeometryType.POINT, geometry, array); break; case MULTIPOLYGON: parseCollection(GeometryType.POLYGON, geometry, array); break; case POLYGON: parseCollection(GeometryType.LINEARRING, geometry, array); break; default: break; } return geometry; } return null; }
From source file:org.geomajas.gwt2.plugin.wms.client.service.WmsServiceImpl.java
License:Open Source License
@Override public void getFeatureInfo(ViewPort viewPort, final WmsLayer wmsLayer, Coordinate location, String format, final Callback<List<Feature>, String> callback) { final String url = getFeatureInfoUrl(viewPort, wmsLayer, location, format.toString()); // we can only handle json for now if (!GetFeatureInfoFormat.JSON.toString().equals(format)) { callback.onFailure("Client does not support " + format + " format"); }// w ww.j a va2s . c om RequestBuilder builder = requestBuilderFactory.create(RequestBuilder.GET, url); try { builder.sendRequest(null, new RequestCallback() { public void onError(Request request, Throwable e) { callback.onFailure(e.getMessage()); } public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { JSONValue jsonValue = JsonService.parse(response.getText()); FeatureCollection featureCollection; if (jsonValue.isObject() != null) { if (wmsLayer instanceof FeaturesSupported) { FeaturesSupported featuresSupported = (FeaturesSupported) wmsLayer; featureCollection = jsonFeatureFactory.createCollection(jsonValue.isObject(), featuresSupported); } else { featureCollection = jsonFeatureFactory.createCollection(jsonValue.isObject(), null); } callback.onSuccess(featureCollection.getFeatures()); } else if (jsonValue.isNull() != null) { callback.onFailure("Response was empty"); } } else { callback.onFailure(response.getText()); } } }); } catch (RequestException e) { // Couldn't connect to server callback.onFailure(e.getMessage()); } }
From source file:org.geowe.client.local.initializer.GeoMapInitializer.java
License:Open Source License
private void showW3WPosition(final EventObject eventObject) { final ProgressBarDialog progressBar = new ProgressBarDialog(false, UIMessages.INSTANCE.processing()); progressBar.show();/*from w w w.j ava 2 s . c o m*/ final LonLat internalLonlat = getLonLat(eventObject); final LonLat lonlat = getLonLat(eventObject); lonlat.transform(GeoMap.INTERNAL_EPSG, "WGS84"); final String position = lonlat.lat() + "," + lonlat.lon(); w3wTool.set3Words(UIMessages.INSTANCE.processing()); w3wServiceAsync.get3Words(position, w3wTool.getLocale(), new AsyncCallback<String>() { public void onFailure(final Throwable caught) { progressBar.hide(); final AlertMessageBox messageBox = new AlertMessageBox(UIMessages.INSTANCE.warning(), UIMessages.INSTANCE.w3wErrorText()); messageBox.show(); w3wTool.set3Words(UIMessages.INSTANCE.w3wErrorText()); w3wTool.addElementToW3wLayer(internalLonlat, (UIMessages.INSTANCE.w3wErrorText())); } public void onSuccess(final String response) { progressBar.hide(); if (response.isEmpty()) { showException(UIMessages.INSTANCE.w3wErrorText()); final String wordsW3W = UIMessages.INSTANCE.w3wErrorText(); w3wTool.set3Words(wordsW3W); w3wTool.addElementToW3wLayer(internalLonlat, wordsW3W); return; } final JSONValue jsonValue = JSONParser.parseLenient(response); final JSONObject jsonObject = jsonValue.isObject(); if (jsonObject.containsKey("words")) { final String wordsW3W = jsonObject.get("words").isString().stringValue(); w3wTool.set3Words(wordsW3W); w3wTool.addElementToW3wLayer(internalLonlat, wordsW3W); } else if (jsonObject.containsKey("error")) { showException("Error returned from w3w API: " + jsonObject.get("message").toString()); } else { showException("Undefined error while fetching words by position"); } } }); }
From source file:org.geowe.client.local.main.GeocodingPanelWidget.java
License:Open Source License
private void getW3WPosition(final String words) { startProgressBar();//from ww w .j a v a2 s . c o m w3wServiceAsync.getPosition(words, w3wTool.getLocale(), new AsyncCallback<String>() { public void onFailure(final Throwable caught) { finishProgressBar(); final AlertMessageBox messageBox = new AlertMessageBox(UIMessages.INSTANCE.warning(), UIMessages.INSTANCE.w3wErrorText()); messageBox.show(); } public void onSuccess(final String response) { finishProgressBar(); if (response.isEmpty()) { showException(UIMessages.INSTANCE.w3wErrorText()); return; } final JSONValue jsonValue = JSONParser.parseLenient(response); final JSONObject jsonObject = jsonValue.isObject(); if (jsonObject.containsKey("geometry")) { final JSONObject jsonCoords = jsonObject.get("geometry").isObject(); final double latitud = jsonCoords.get("lat").isNumber().doubleValue(); final double longitud = jsonCoords.get("lng").isNumber().doubleValue(); updateMap(latitud, longitud, 20, "EPSG:4326"); final LonLat lonLat = new LonLat(longitud, latitud); transformToInternalProjection(lonLat, "EPSG:4326"); w3wTool.addElementToW3wLayer(lonLat, words); } else if (jsonObject.containsKey("error")) { showException(UIMessages.INSTANCE.fail() + jsonObject.get("message").toString()); } else { showException(UIMessages.INSTANCE.w3wErrorText()); } } }); }