Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum support #140

Merged
merged 3 commits into from
Jul 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ of this software and associated documentation files (the "Software"), to deal
* </ul>
*
* @author JSON.org
* @version 2015-07-06
* @version 2015-07-22
*/
public class JSONArray implements Iterable<Object> {

Expand Down Expand Up @@ -247,6 +247,31 @@ public double getDouble(int index) throws JSONException {
}
}

/**
* 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;
}

/**
* Get the BigDecimal value associated with an index.
*
Expand Down Expand Up @@ -531,6 +556,50 @@ public int optInt(int index, int defaultValue) {
}
}

/**
* 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.
* @return The enum value at the index location or null if not found
*/
public <E extends Enum<E>> E optEnum(Class<E> clazz, int index) {
return this.optEnum(clazz, index, null);
}

/**
* 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 BigInteger value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the
Expand Down
70 changes: 69 additions & 1 deletion JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ of this software and associated documentation files (the "Software"), to deal
* </ul>
*
* @author JSON.org
* @version 2015-07-06
* @version 2015-07-22
*/
public class JSONObject {
/**
Expand Down Expand Up @@ -479,6 +479,31 @@ public Object get(String key) throws JSONException {
return object;
}

/**
* Get the enum value associated with a key.
*
* @param clazz
* The type of enum to retrieve.
* @param key
* A key string.
* @return The enum value associated with the key
* @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, String key) throws JSONException {
E val = optEnum(clazz, key);
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[" + quote(key)
+ "] is not an enum of type " + quote(clazz.getSimpleName())
+ ".");
}
return val;
}

/**
* Get the boolean value associated with a key.
*
Expand Down Expand Up @@ -844,6 +869,49 @@ public Object opt(String key) {
return key == null ? null : this.map.get(key);
}

/**
* Get the enum value associated with a key.
*
* @param clazz
* The type of enum to retrieve.
* @param key
* A key string.
* @return The enum value associated with the key or null if not found
*/
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key) {
return this.optEnum(clazz, key, null);
}

/**
* Get the enum value associated with a key.
*
* @param clazz
* The type of enum to retrieve.
* @param key
* A key string.
* @param defaultValue
* The default in case the value is not found
* @return The enum value associated with the key or defaultValue
* if the value is not found or cannot be assigned to clazz
*/
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue) {
try {
Object val = this.opt(key);
if (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 an optional boolean associated with a key. It returns false if there
* is no such key, or if the value is not Boolean.TRUE or the String "true".
Expand Down