static void throwJSONException(String message) { throw new JSONException(message); }
/** Returns commands for the specified {@code project}. */ List<CommandImpl> getCommands(Project project) { List<String> attrValues = project.getAttributes(COMMANDS_ATTRIBUTE_NAME); if (attrValues == null) { return new ArrayList<>(); } Map<String, CommandImpl> commands = new HashMap<>(attrValues.size()); for (String commandJson : attrValues) { try { CommandDto commandDto = dtoFactory.createDtoFromJson(commandJson, CommandDto.class); commands.put(commandDto.getName(), new CommandImpl(commandDto)); } catch (JSONException e) { Log.error( ProjectCommandManagerDelegate.class, "Unable to parse command of project '" + project.getPath() + "': " + commandJson + ". " + e.getMessage()); } } return new ArrayList<>(commands.values()); }
static void throwJSONException(String message) { throw new JSONException(message); }
static void throwUnknownTypeException(String typeString) { throw new JSONException("Unexpected typeof result '" + typeString + "'; please report this bug to the GWT team"); }
static void throwUnknownTypeException(String typeString) { throw new JSONException("Unexpected typeof result '" + typeString + "'; please report this bug to the GWT team"); }
private static void checkArguments(JSONObject jsonObject, String key) throws JSONException { if (key == null) { throw new IllegalArgumentException("Can't go searching for a null key."); } if (jsonObject == null) { throw new JSONException("Can't search for key '" + key + "' in a null object."); } if (!jsonObject.containsKey(key)) { throw new JSONException("Key '" + key + "' could not be found."); } // Please, proceed... }
private static JSONValue parse(String jsonString, boolean strict) { if (jsonString == null) { throw new NullPointerException(); } if (jsonString.length() == 0) { throw new IllegalArgumentException("empty argument"); } try { return evaluate(jsonString, strict); } catch (JavaScriptException ex) { throw new JSONException(ex); } }
private static JSONValue parse(String jsonString, boolean strict) { if (jsonString == null) { throw new NullPointerException(); } if (jsonString.length() == 0) { throw new IllegalArgumentException("empty argument"); } try { return evaluate(jsonString, strict); } catch (JavaScriptException ex) { throw new JSONException(ex); } }
@Override public T read(String jsonString) { T model = null; if (jsonString != null && jsonString.trim().length() != 0) { JSONObject jsonObject = JSONParser.parseStrict(jsonString).isObject(); if (jsonObject != null) { model = internalRead(jsonObject); } else { throw new JSONException("\"" + jsonString + "\" represents invalid json data!"); } } return model; }
/** * Add a certain object value to the given JSON object. * * @param jsonObject The JSON object to add the value to. * @param key The key to be used when adding the value. * @param value The value to attach to the key in the JSON object. * @throws JSONException In case something went wrong while adding. */ public static void addToJson(JSONObject jsonObject, String key, Object value) throws JSONException { if (jsonObject == null) { throw new JSONException("Can't add key '" + key + "' to a null object."); } if (key == null) { throw new JSONException("Can't add null key."); } JSONValue jsonValue = null; if (value != null) { if (value instanceof Date) { jsonValue = new JSONString(JsonService.format((Date) value)); } else if (value instanceof JSONValue) { jsonValue = (JSONValue) value; } else { jsonValue = new JSONString(value.toString()); } } jsonObject.put(key, jsonValue); }
return new Date(millis); } catch (Exception e2) { throw new JSONException("Could not parse the date for key '" + key + "'", e);
throw new JSONException("\"" + jsonString + "\" represents invalid json data!");
/** * Returns a Key instance based on the given JSON string. * * @param em * The entity manager that can be used to look up the entity type * corresponding with the key. * @param key * The key to parse. * @param failIfNotFound * If true, and the entity type given in {@code key} is not known to * {@code em}, an IllegalArgumentException will be thrown. * @return An instance of Key that corresponds with the entity type and ID of * the given JSON string. */ public static Key<?, ?> fromJson(ErraiEntityManager em, String key, boolean failIfNotFound) { JSONValue k; try { k = JSONParser.parseStrict(key); } catch (JSONException e) { throw new JSONException("Input: " + key, e); } return fromJsonObject(em, k.isObject(), failIfNotFound); }
@Override public List<T> readList(String jsonString, String arrayKey) { List<T> models = null; if (jsonString != null && jsonString.trim().length() != 0) { JSONObject jsonObject = JSONParser.parseStrict(jsonString).isObject(); if (jsonObject != null) { if (arrayKey != null) { JSONValue jsonValue = jsonObject.get(arrayKey); if (jsonValue != null) { JSONArray jsonArray = jsonValue.isArray(); if (jsonArray != null) { models = readList(jsonArray); } } } } else { throw new JSONException("\"" + jsonString + "\" represents invalid json data!"); } } return models; }
/** * Get a child JSON object from a parent JSON object. * * @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; }
/** * Get a child JSON array from a parent JSON object. * * @param jsonObject The parent JSON object. * @param key The name of the child object. * @return Returns the child JSON array 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 JSONArray getChildArray(JSONObject jsonObject, String key) throws JSONException { checkArguments(jsonObject, key); JSONValue value = jsonObject.get(key); if (value != null) { if (value.isArray() != null) { return value.isArray(); } else if (value.isNull() != null) { return null; } throw new JSONException("Child is not a JSONArray, but a: " + value.getClass()); } return null; }