List of usage examples for com.google.gwt.json.client JSONValue isString
public JSONString isString()
From source file:org.waveprotocol.wave.client.gadget.renderer.GadgetUserPrefs.java
License:Apache License
/** * Extracts default preference values from the gadget metadata JSON object * returned from GGS.// w ww. ja v a 2 s.c o m * * @param prefs the preference JSON object received from GGS. */ public void parseDefaultValues(JSONObject prefs) { if (prefs != null) { for (String pref : prefs.keySet()) { if (!has(pref)) { JSONObject prefJson = prefs.get(pref).isObject(); if (prefJson != null) { JSONValue value = prefJson.get("default"); if ((value != null) && (value.isString() != null)) { put(pref, value.isString().stringValue()); log("Gadget pref '" + pref + "' = '" + get(pref) + "'"); } } else { log("Invalid pref '" + pref + "' value in Gadget metadata."); } } } } }
From source file:org.xwiki.gwt.wysiwyg.client.MenuItemDescriptorJSONParser.java
License:Open Source License
/** * Creates a menu item descriptor from the given JSON value. * /*from w ww.j av a2 s.co m*/ * @param value a JSON value * @return a menu item descriptor */ private MenuItemDescriptor getMenuItemDescriptor(JSONValue value) { JSONString feature = value.isString(); if (feature != null) { return new MenuItemDescriptor(feature.stringValue()); } else { JSONObject jsDescriptor = value.isObject(); if (jsDescriptor == null) { return null; } JSONValue oFeature = jsDescriptor.get("feature"); if (oFeature == null || oFeature.isString() == null) { return null; } MenuItemDescriptor descriptor = new MenuItemDescriptor(oFeature.isString().stringValue()); JSONValue subMenu = jsDescriptor.get("subMenu"); if (subMenu != null) { descriptor.getSubMenu().addAll(getMenuItemDescriptors(subMenu)); } return descriptor; } }
From source file:org.xwiki.gwt.wysiwyg.client.plugin.style.StyleDescriptorJSONParser.java
License:Open Source License
/** * Creates a new style descriptor from a JSON object representing a style descriptor. * //ww w . j a v a 2 s .c o m * @param jsDescriptor a JSON object representing a style descriptor * @return a new style descriptor matching the given JSON object */ private StyleDescriptor createStyleDescriptor(JSONObject jsDescriptor) { JSONValue oName = jsDescriptor.get("name"); if (oName == null) { return null; } JSONString sName = oName.isString(); if (sName == null) { return null; } String name = sName.stringValue().trim(); if (name.length() == 0) { return null; } JSONValue oLabel = jsDescriptor.get("label"); JSONString sLabel = oLabel != null ? oLabel.isString() : null; String label = sLabel != null ? sLabel.stringValue().trim() : name; JSONValue inline = jsDescriptor.get("inline"); JSONBoolean bInline = inline != null ? inline.isBoolean() : null; return new StyleDescriptor(name, label, bInline != null ? bInline.booleanValue() : true); }
From source file:rocket.json.client.CharJsonSerializer.java
License:Apache License
public char read(final JSONValue jsonValue) { char value = 0; while (true) { if (null == jsonValue) { break; }/* ww w. j a va2s .c o m*/ final JSONString c = jsonValue.isString(); if (null == c) { break; } value = c.stringValue().charAt(0); break; } return value; }
From source file:rocket.json.client.StringJsonSerializer.java
License:Apache License
public String read(final JSONValue jsonValue) { String value = null;/*from ww w. jav a 2 s .c o m*/ while (true) { if (null == jsonValue) { break; } final JSONString jsonString = jsonValue.isString(); if (null == jsonString) { break; } value = jsonString.stringValue(); break; } return value; }
From source file:rpc.client.data.JSONSerializer.java
License:Open Source License
private Object fromJSONValue(JSONValue jsonValue, Type expected) throws NoSuitableSerializableFactory { if (jsonValue == null) { return null; }/*from www . j av a 2 s . c om*/ // Null JSONNull asNull = jsonValue.isNull(); if (asNull != null) { return null; } // Boolean JSONBoolean asBoolean = jsonValue.isBoolean(); if (asBoolean != null) { return asBoolean.booleanValue(); } // Integer // Long // Float // Double JSONNumber asNumber = jsonValue.isNumber(); if (asNumber != null) { double value = asNumber.doubleValue(); if (expected.isInteger()) { return (int) value; } if (expected.isLong()) { return (long) value; } if (expected.isFloat()) { return (float) value; } if (expected.isDouble()) { return value; } } // String // Enum JSONString asString = jsonValue.isString(); if (asString != null) { if (expected.isEnum()) { String value = asString.stringValue(); return Enum.valueOf((Class) expected.getTypeClass(), value); } else { return asString.stringValue(); } } // Map // Serializable JSONObject asObject = jsonValue.isObject(); if (asObject != null) { if (expected.isMap()) { Map<Object, Object> map = new HashMap<Object, Object>(); Type keyType = expected.getParameterized(0); Type valueType = expected.getParameterized(1); if (!(keyType.isString() || keyType.isEnum())) { return null; } for (String key : asObject.keySet()) { JSONValue value = asObject.get(key); if (keyType.isString()) { map.put(key, fromJSONValue(value, valueType)); } if (keyType.isEnum()) { map.put(Enum.valueOf((Class) keyType.getTypeClass(), key), fromJSONValue(value, valueType)); } } return map; } else { if (provider == null) { throw new NoSuitableSerializableFactory(); } Serializable object = provider.make(expected); for (Map.Entry<String, Type> entry : object.fields().entrySet()) { String field = entry.getKey(); Type fieldType = entry.getValue(); JSONValue value = asObject.get(field); object.set(field, fromJSONValue(value, fieldType)); } return object; } } // List JSONArray asArray = jsonValue.isArray(); if (asArray != null) { int size = asArray.size(); List<Object> list = new ArrayList<Object>(); Type itemType = expected.getParameterized(0); for (int i = 0; i < size; i++) { JSONValue value = asArray.get(i); list.add(fromJSONValue(value, itemType)); } return list; } return null; }
From source file:stroom.util.client.JSONUtil.java
License:Apache License
public static String getString(final JSONValue v) { if (v != null) { final JSONString jsonString = v.isString(); if (jsonString != null) { return jsonString.stringValue(); }// ww w .j a v a2 s. co m } return null; }
From source file:tsd.client.QueryUi.java
License:Open Source License
private void refreshGraph() { final Date start = start_datebox.getValue(); if (start == null) { graphstatus.setText("Please specify a start time."); return;/*from w ww .j a v a 2s .c o m*/ } final Date end = end_datebox.getValue(); if (end != null && !autoreload.getValue()) { if (end.getTime() <= start.getTime()) { end_datebox.addStyleName("dateBoxFormatError"); graphstatus.setText("End time must be after start time!"); return; } } final StringBuilder url = new StringBuilder(); url.append("/q?start="); final String start_text = start_datebox.getTextBox().getText(); if (start_text.endsWith(" ago") || start_text.endsWith("-ago")) { url.append(start_text); } else { url.append(FULLDATE.format(start)); } if (end != null && !autoreload.getValue()) { url.append("&end="); final String end_text = end_datebox.getTextBox().getText(); if (end_text.endsWith(" ago") || end_text.endsWith("-ago")) { url.append(end_text); } else { url.append(FULLDATE.format(end)); } } else { // If there's no end-time, the graph may change while the URL remains // the same. No browser seems to re-fetch an image once it's been // fetched, even if we destroy the DOM object and re-created it with the // same src attribute. This has nothing to do with caching headers sent // by the server. The browsers simply won't retrieve the same URL again // through JavaScript manipulations, period. So as a workaround, we add // a special parameter that the server will delete from the query. url.append("&ignore=" + nrequests++); } if (!addAllMetrics(url)) { return; } addLabels(url); addFormats(url); addYRanges(url); addLogscales(url); if (nokey.getValue()) { url.append("&nokey"); } else if (!keypos.isEmpty() || horizontalkey.getValue()) { url.append("&key="); if (!keypos.isEmpty()) { url.append(keypos); } if (horizontalkey.getValue()) { url.append(" horiz"); } if (keybox.getValue()) { url.append(" box"); } } url.append("&wxh=").append(wxh.getText()); if (smooth.getValue()) { url.append("&smooth=csplines"); } final String unencodedUri = url.toString(); final String uri = URL.encode(unencodedUri); if (uri.equals(lastgraphuri)) { return; // Don't re-request the same graph. } else if (pending_requests++ > 0) { return; } lastgraphuri = uri; graphstatus.setText("Loading graph..."); asyncGetJson(uri + "&json", new GotJsonCallback() { public void got(final JSONValue json) { if (autoreoload_timer != null) { autoreoload_timer.cancel(); autoreoload_timer = null; } final JSONObject result = json.isObject(); final JSONValue err = result.get("err"); String msg = ""; if (err != null) { displayError("An error occurred while generating the graph: " + err.isString().stringValue()); graphstatus.setText("Please correct the error above."); } else { clearError(); String history = unencodedUri.substring(3) // Remove "/q?". .replaceFirst("ignore=[^&]*&", ""); // Unnecessary cruft. if (autoreload.getValue()) { history += "&autoreload=" + autoreoload_interval.getText(); } if (!history.equals(URL.decode(History.getToken()))) { History.newItem(history, false); } final JSONValue nplotted = result.get("plotted"); final JSONValue cachehit = result.get("cachehit"); if (cachehit != null) { msg += "Cache hit (" + cachehit.isString().stringValue() + "). "; } if (nplotted != null && nplotted.isNumber().doubleValue() > 0) { graph.setUrl(uri + "&png"); graph.setVisible(true); msg += result.get("points").isNumber() + " points retrieved, " + nplotted + " points plotted"; } else { graph.setVisible(false); msg += "Your query didn't return anything"; } final JSONValue timing = result.get("timing"); if (timing != null) { msg += " in " + timing + "ms."; } else { msg += '.'; } } final JSONValue info = result.get("info"); if (info != null) { if (!msg.isEmpty()) { msg += ' '; } msg += info.isString().stringValue(); } graphstatus.setText(msg); if (result.get("etags") != null) { final JSONArray etags = result.get("etags").isArray(); final int netags = etags.size(); for (int i = 0; i < netags; i++) { if (i >= metrics.getWidgetCount()) { break; } final Widget widget = metrics.getWidget(i); if (!(widget instanceof MetricForm)) { break; } final MetricForm metric = (MetricForm) widget; final JSONArray tags = etags.get(i).isArray(); // Skip if no tags were associated with the query. if (null != tags) { for (int j = 0; j < tags.size(); j++) { metric.autoSuggestTag(tags.get(j).isString().stringValue()); } } } } if (autoreload.getValue()) { final int reload_in = Integer.parseInt(autoreoload_interval.getValue()); if (reload_in >= 5) { autoreoload_timer = new Timer() { public void run() { // Verify that we still want auto reload and that the graph // hasn't been updated in the mean time. if (autoreload.getValue() && lastgraphuri == uri) { // Force refreshGraph to believe that we want a new graph. lastgraphuri = ""; refreshGraph(); } } }; autoreoload_timer.schedule(reload_in * 1000); } } if (--pending_requests > 0) { pending_requests = 0; refreshGraph(); } } }); }
From source file:tsd.client.RemoteOracle.java
License:Open Source License
@Override public void requestSuggestions(final Request request, final Callback callback) { if (current != null) { pending_req = request;//from w ww . j a v a2 s . co m pending_cb = callback; return; } current = callback; { final String this_query = request.getQuery(); // Check if we can serve this from our local cache, without even talking // to the server. This is possible if either of those is true: // 1. We've already seen this query recently. // 2. This new query precedes another one and the user basically just // typed another letter, so if the new query is "less than" the last // result we got from the server, we know we already cached the full // range of results covering the new request. if ((last_query != null && last_query.compareTo(this_query) <= 0 && this_query.compareTo(last_suggestion) < 0) || queries_seen.check(this_query)) { current = null; cache.requestSuggestions(request, callback); return; } last_query = this_query; } final RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, SUGGEST_URL + type + "&q=" + last_query); try { builder.sendRequest(null, new RequestCallback() { public void onError(final com.google.gwt.http.client.Request r, final Throwable e) { current = null; // Something bad happened, drop the current request. if (pending_req != null) { // But if we have another waiting... requestSuggestions(pending_req, pending_cb); // ... try it now. } } // Need to use fully-qualified names as this class inherits already // from a pair of inner classes called Request / Response :-/ public void onResponseReceived(final com.google.gwt.http.client.Request r, final com.google.gwt.http.client.Response response) { if (response.getStatusCode() == com.google.gwt.http.client.Response.SC_OK) { final JSONValue json = JSONParser.parse(response.getText()); // In case this request returned nothing, we pretend the last // suggestion ended with the largest character possible, so we // won't send more requests to the server if the user keeps // adding extra characters. last_suggestion = last_query + "\377"; if (json != null && json.isArray() != null) { final JSONArray results = json.isArray(); final int n = Math.min(request.getLimit(), results.size()); for (int i = 0; i < n; i++) { final JSONValue suggestion = results.get(i); if (suggestion == null || suggestion.isString() == null) { continue; } final String suggestionstr = suggestion.isString().stringValue(); last_suggestion = suggestionstr; cache.add(suggestionstr); } // Is this response still relevant to what the requester wants? if (requester.getText().startsWith(last_query)) { cache.requestSuggestions(request, callback); pending_req = null; pending_cb = null; } } } current = null; // Regardless of what happened above, this is done. if (pending_req != null) { final Request req = pending_req; final Callback cb = pending_cb; pending_req = null; pending_cb = null; requestSuggestions(req, cb); } } }); } catch (RequestException ignore) { } }