List of usage examples for com.google.gwt.json.client JSONException JSONException
public JSONException(Throwable cause)
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();//from w ww. java 2 s . c o 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();// ww w . ja va2s . c om 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.gerrit.client.rpc.RestApi.java
License:Apache License
private static JSONValue parseJson(Response res) throws JSONException { String json = trimJsonMagic(res.getText()); if (json.isEmpty()) { throw new JSONException("response was empty"); }// www . j ava 2s.c o m return JSONParser.parseStrict(json); }
From source file:com.google.gerrit.client.rpc.RestApi.java
License:Apache License
@SuppressWarnings("unchecked") private static <T extends JavaScriptObject> T cast(JSONValue val) { if (val.isObject() != null) { return (T) val.isObject().getJavaScriptObject(); } else if (val.isArray() != null) { return (T) val.isArray().getJavaScriptObject(); } else if (val.isString() != null) { return (T) NativeString.wrap(val.isString().stringValue()); } else if (val.isNull() != null) { return null; } else {// w ww . ja v a 2 s. co m throw new JSONException("unsupported JSON type"); } }
From source file:com.rhizospherejs.gwt.client.bridge.JSONStringModelBridge.java
License:Open Source License
@Override public JavaScriptObject bridgeInternal(String model, JsoBuilder builder) { JSONObject jsonModel = JSONParser.parseStrict(model).isObject(); if (jsonModel == null) { throw new JSONException("Received model is not a JSON Object (" + model + ")"); }/* www .j a v a 2s. c o m*/ return jsonModel.getJavaScriptObject(); }
From source file:org.apache.solr.explorer.client.util.json.JSONUtils.java
License:Apache License
public static Date dateFieldValue(JSONObject object, String key, String[] patterns) throws JSONException { for (String pattern : patterns) { try {//from w w w . j a va 2 s . c om return dateFieldValue(object, key, pattern); } catch (Exception e) { } } throw new JSONException("Unabled to parse date '" + String.valueOf(object) + "'. Unknown format"); }
From source file:org.apache.solr.explorer.client.util.json.JSONUtils.java
License:Apache License
/** * Converts JSON value to integer.//from w ww. j a va 2 s . c o m * * @param jsonValue the JSON value. * @return the integer value. */ public static Integer jsonValueToInt(JSONValue jsonValue) { JSONNumber number = jsonValue.isNumber(); if (number == null) { throw new JSONException("Not an integer: " + jsonValue); } else { return new Double(number.doubleValue()).intValue(); } }
From source file:org.apache.solr.explorer.client.util.json.JSONUtils.java
License:Apache License
/** * Converts JSON value to long.//from w w w . j av a 2 s. co m * * @param jsonValue the JSON value. * @return the integer value. */ public static Long jsonValueToLong(JSONValue jsonValue) { JSONNumber number = jsonValue.isNumber(); if (number == null) { throw new JSONException("Not a long: " + jsonValue); } else { return new Double(number.doubleValue()).longValue(); } }
From source file:org.apache.solr.explorer.client.util.json.JSONUtils.java
License:Apache License
public static Boolean jsonValueToBoolean(JSONValue jsonValue) { JSONBoolean bool = jsonValue.isBoolean(); if (bool != null) { return bool.booleanValue(); }/* w w w.j a va 2 s.c om*/ JSONString string = jsonValue.isString(); if (string != null) { return "true".equals(string.stringValue()) || "t".equals(string.stringValue()) || "yes".equals(string.stringValue()) || "y".equals(string.stringValue()); } throw new JSONException("Not a boolean: " + jsonValue); }
From source file:org.geomajas.gwt2.client.service.JsonService.java
License:Open Source License
/** * Get a child JSON object from a parent JSON object. * /*from www . ja v a 2 s. com*/ * @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; }