List of usage examples for com.google.gwt.json.client JSONValue toString
@Override public abstract String toString();
From source file:ch.gbrain.gwtstorage.manager.StorageManager.java
License:Apache License
/** * Write the given item to the local HTML5 storage. * /*ww w. j a v a 2s . c o m*/ * @param item The object to be serialized and stored under the ID within the * local storage * @return true if the write operation succeeded */ public boolean writeStorageItemToLocalStorage(StorageItem item) { if (item == null) return false; try { JSONValue json = item.toJson(); getLocalStorage().setItem(item.getStorageItemIdKey(), json.toString()); writeStorageItemStorageTimeToLocalStorage(item); logger.log(Level.INFO, "Local StorageItem written" + item.getLogId()); return true; } catch (Exception ex) { logger.log(Level.SEVERE, "Failure local write" + item.getLogId(), ex); } return false; }
From source file:ch.gbrain.gwtstorage.manager.StorageManager.java
License:Apache License
/** * Write the item to the local fileentry asynchronous * /*from w ww. ja v a2s .c om*/ * @param fileEntry * @param item * @param callback Is called once the asynch action completed or failed * @return false if the asynchronous action invocation failed. */ private boolean writeStorageItemToLocalFile(FileEntry fileEntry, final StorageItem item, final Callback<StorageItem, StorageError> callback) { if (item == null) return false; try { logger.log(Level.INFO, "writeStorageItem to local file invoked" + item.getLogId()); fileEntry.createWriter(new FileCallback<FileWriter, FileError>() { @Override public void onSuccess(FileWriter writer) { writer.setOnWriteEndCallback(new WriterCallback<FileWriter>() { @Override public void onCallback(FileWriter result) { // file written logger.log(Level.INFO, "writeToLocalFile successfully written" + item.getLogId()); if (callback != null) { callback.onSuccess(item); } } }); writer.setOnErrorCallback(new WriterCallback<FileWriter>() { @Override public void onCallback(FileWriter result) { // Error while writing file logger.log(Level.SEVERE, "Failure file write StorageItem" + item.getLogId() + " : " + result.toString()); if (callback != null) { callback.onFailure(new StorageError(result.getError())); } } }); JSONValue json = item.toJson(); writer.write(json.toString()); } @Override public void onFailure(FileError error) { // can not create writer logger.log(Level.SEVERE, "Failure file writer creation StorageItem" + item.getLogId() + " : " + error.toString()); if (callback != null) { callback.onFailure(new StorageError(error)); } } }); return true; } catch (Exception ex) { logger.log(Level.SEVERE, "Exception file write StorageItem" + item.toString(), ex); if (callback != null) { callback.onFailure(new StorageError(FileError.ABORT_ERR)); } } return false; }
From source file:com.google.api.explorer.client.parameter.schema.ArraySchemaEditor.java
License:Apache License
@Override public void setJSONValue(JSONValue value) { JSONArray arr = value.isArray();//w w w . j av a 2 s.co m if (arr != null) { for (int i = 0; i < arr.size(); i++) { // We may have to create additional editors if (i >= editors.size()) { addItem(); } SchemaEditor editor = editors.get(i); editor.setJSONValue(arr.get(i)); } } else { throw new JSONException("Not a valid JSON array: " + value.toString()); } }
From source file:com.google.api.explorer.client.parameter.schema.ObjectSchemaEditor.java
License:Apache License
@Override public void setJSONValue(JSONValue value) { JSONObject obj = value.isObject();//from w w w . ja v a2 s .com if (obj == null) { // If this object came as a json blob, we might have to deserialize it JSONString str = value.isString(); JSONValue parsed = null; try { parsed = JSONParser.parseStrict(str.stringValue()); } catch (Exception e) { // There was an error parsing, just leave parsed as null } JSONObject parsedObject = parsed != null ? parsed.isObject() : null; if (parsedObject != null) { obj = parsed.isObject(); } } if (obj != null) { // Clear the editor before we start adding the keys back in clear(); // For each key that we are going to map we have to instantiate an // appropriate editor type. The {@link #onSelect(String)} function // instantiates the proper editor type for the key and binds the new // editor to our editor. for (String key : obj.keySet()) { if (properties.containsKey(key)) { SchemaEditor editor = onSelect(key, OPTIONAL_PROPERTY); editor.setJSONValue(obj.get(key)); } else if (additionalPropertiesType != null) { SchemaEditor editor = addAdditionalPropertyEditor(key); editor.setJSONValue(obj.get(key)); } else { throw new JSONException("JSON object contains unknown key: " + key); } } } else { throw new JSONException("Invalid JSON object: " + value.toString()); } }
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.c o 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 w w . j a va 2s. c o m 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.hiramchirino.restygwt.client.Method.java
License:Apache License
public Method json(JSONValue data) { defaultContentType(Resource.CONTENT_TYPE_JSON); builder.setRequestData(data.toString()); return this; }
From source file:com.hiramchirino.restygwt.examples.client.JsonpTestGWT.java
License:Apache License
private JsonCallback expectJsonIsSetTo(final JSONObject expected) { return new JsonCallback() { public void onSuccess(Method method, JSONValue response) { System.out.println("Got: " + response.toString()); assertEquals(expected.toString(), response.toString()); // finishTest(); }/*from w ww . ja v a 2s . c om*/ public void onFailure(Method method, Throwable exception) { fail(exception.getMessage()); } }; }
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 www.java 2 s.c om*/ 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.nitrous.gwt.earth.client.api.GoogleEarthWidget.java
License:Apache License
/** * Initialize the Google Earth Plugin with the additional specified parameters * @param jsonInitParams The JSON encoded initialization parameters to be passed to the google earth plugin * <pre>// ww w.ja v a 2s . c o m * Supported parameters are: * <li>database - The URL of an alternative Earth Enterprise database to connect to instead of the default database. * Note: Certain changes may be required to your Google Earth Server configuration before Google Earth Plugin instances will be able to connect to it using this method. Google Earth Server versions 3.2 and higher are already pre-configured for connectivity with the plugin. Contact Google Earth Enterprise support for more details. * Note: Keep in mind the Google Earth API Terms of Service while using this parameter.</li> * * <li>language - The language code specifying the language to use for road and border labels, * Terms of Use text, and error messages. * Supported language codes are listed in the Google Maps API Coverage document.</li> * </pre> */ public void init(JSONValue jsonInitParams) { String str = jsonInitParams.toString(); JavaScriptObject json = toJson(str); GoogleEarth.createInstance(containerId, json, new GoogleEarthInitListener() { @Override public void onSuccess(GEPlugin plugin) { onInitSuccess(plugin); } @Override public void onFailure(String cause) { onInitFailure(cause); } }); }