/** * Get the enum value associated with an index. * * @param clazz The type of enum to retrieve. * @param index The index must be between 0 and length() - 1. * @return The enum value at the index location * @throws JSONException if the key is not found or if the value cannot be converted to an enum. */ public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException { E val = optEnum(clazz, index); if (val == null) { // JSONException should really take a throwable argument. // If it did, I would re-implement this with the Enum.valueOf // method and place any thrown exception in the JSONException throw new JSONException( "JSONObject[" + JSONObject.quote(Integer.toString(index)) + "] is not an enum of type " + JSONObject.quote(clazz.getSimpleName()) + "."); } return val; }
public String toJSONString() { return "{" + JSONObject.quote(this.aString) + ":" + JSONObject.doubleToString(this.aNumber) + "}"; }
static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent) throws JSONException, IOException { if (value == null || value.equals(null)) { writer.write("null"); } else if (value instanceof JSONObject) { ((JSONObject) value).write(writer, indentFactor, indent); } else if (value instanceof JSONArray) { ((JSONArray) value).write(writer, indentFactor, indent); } else if (value instanceof Map) { new JSONObject((Map) value).write(writer, indentFactor, indent); } else if (value instanceof Collection) { new JSONArray((Collection) value).write(writer, indentFactor, indent); } else if (value.getClass().isArray()) { new JSONArray(value).write(writer, indentFactor, indent); } else if (value instanceof Number) { writer.write(numberToString((Number) value)); } else if (value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof JSONString) { Object o; try { o = ((JSONString) value).toJSONString(); } catch (Exception e) { throw new JSONException(e); } writer.write(o != null ? o.toString() : quote(value.toString())); } else { quote(value.toString(), writer); } return writer; }
/** * Append a key. The key will be associated with the next value. In an object, every value must be * preceded by a key. * * @param s A key string. * @return this * @throws JSONException If the key is out of place. For example, keys do not belong in arrays or * if the key is null. */ public JSONWriter key(String s) throws JSONException { if (s == null) { throw new JSONException("Null key."); } if (this.mode == 'k') { try { if (this.comma) { this.writer.write(','); } this.writer.write(JSONObject.quote(s)); this.writer.write(':'); this.comma = false; this.mode = 'o'; return this; } catch (IOException e) { throw new JSONException(e); } } throw new JSONException("Misplaced key."); }
/** * Append a key. The key will be associated with the next value. In an object, every value must be * preceded by a key. * * @param string A key string. * @return this * @throws JSONException If the key is out of place. For example, keys do not belong in arrays or * if the key is null. */ public JSONWriter key(String string) throws JSONException { if (string == null) { throw new JSONException("Null key."); } if (mode == 'k') { try { stack[top - 1].putOnce(string, Boolean.TRUE); if (comma) { writer.write(','); } writer.write(JSONObject.quote(string)); writer.write(':'); comma = false; mode = 'o'; return this; } catch (IOException e) { throw new JSONException(e); } } throw new JSONException("Misplaced key."); }