/** * Get the enum value associated with a key. * * @param clazz The type of enum to retrieve. * @param index The index must be between 0 and length() - 1. * @param defaultValue The default in case the value is not found * @return The enum value at the index location or defaultValue if the value is not found or * cannot be assigned to clazz */ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue) { try { Object val = this.opt(index); if (JSONObject.NULL.equals(val)) { return defaultValue; } if (clazz.isAssignableFrom(val.getClass())) { // we just checked it! @SuppressWarnings("unchecked") E myE = (E) val; return myE; } return Enum.valueOf(clazz, val.toString()); } catch (IllegalArgumentException | NullPointerException e) { return defaultValue; } }
/** * Get the optional string associated with an index. The defaultValue is returned if the key is * not found. * * @param index The index must be between 0 and length() - 1. * @param defaultValue The default value. * @return A String value. */ public String optString(int index, String defaultValue) { Object object = this.opt(index); return JSONObject.NULL.equals(object) ? defaultValue : object.toString(); }
/** * Determine if the value is null. * * @param index The index must be between 0 and length() - 1. * @return true if the value at the index is null, or if there is no value. */ public boolean isNull(int index) { return JSONObject.NULL.equals(this.opt(index)); }