Skip to content

Commit ce2de45

Browse files
committed
Merge pull request stleary#140 from douglascrockford/enum-support
Enum support
2 parents 96b2e38 + 5fc22e3 commit ce2de45

File tree

2 files changed

+139
-2
lines changed

2 files changed

+139
-2
lines changed

JSONArray.java

+70-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ of this software and associated documentation files (the "Software"), to deal
7676
* </ul>
7777
*
7878
* @author JSON.org
79-
* @version 2015-07-06
79+
* @version 2015-07-22
8080
*/
8181
public class JSONArray implements Iterable<Object> {
8282

@@ -247,6 +247,31 @@ public double getDouble(int index) throws JSONException {
247247
}
248248
}
249249

250+
/**
251+
* Get the enum value associated with an index.
252+
*
253+
* @param clazz
254+
* The type of enum to retrieve.
255+
* @param index
256+
* The index must be between 0 and length() - 1.
257+
* @return The enum value at the index location
258+
* @throws JSONException
259+
* if the key is not found or if the value cannot be converted
260+
* to an enum.
261+
*/
262+
public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException {
263+
E val = optEnum(clazz, index);
264+
if(val==null) {
265+
// JSONException should really take a throwable argument.
266+
// If it did, I would re-implement this with the Enum.valueOf
267+
// method and place any thrown exception in the JSONException
268+
throw new JSONException("JSONObject[" + JSONObject.quote(Integer.toString(index))
269+
+ "] is not an enum of type " + JSONObject.quote(clazz.getSimpleName())
270+
+ ".");
271+
}
272+
return val;
273+
}
274+
250275
/**
251276
* Get the BigDecimal value associated with an index.
252277
*
@@ -531,6 +556,50 @@ public int optInt(int index, int defaultValue) {
531556
}
532557
}
533558

559+
/**
560+
* Get the enum value associated with a key.
561+
*
562+
* @param clazz
563+
* The type of enum to retrieve.
564+
* @param index
565+
* The index must be between 0 and length() - 1.
566+
* @return The enum value at the index location or null if not found
567+
*/
568+
public <E extends Enum<E>> E optEnum(Class<E> clazz, int index) {
569+
return this.optEnum(clazz, index, null);
570+
}
571+
572+
/**
573+
* Get the enum value associated with a key.
574+
*
575+
* @param clazz
576+
* The type of enum to retrieve.
577+
* @param index
578+
* The index must be between 0 and length() - 1.
579+
* @param defaultValue
580+
* The default in case the value is not found
581+
* @return The enum value at the index location or defaultValue if
582+
* the value is not found or cannot be assigned to clazz
583+
*/
584+
public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue) {
585+
try {
586+
Object val = this.opt(index);
587+
if (JSONObject.NULL.equals(val)) {
588+
return defaultValue;
589+
}
590+
if (clazz.isAssignableFrom(val.getClass())) {
591+
// we just checked it!
592+
@SuppressWarnings("unchecked")
593+
E myE = (E) val;
594+
return myE;
595+
}
596+
return Enum.valueOf(clazz, val.toString());
597+
} catch (IllegalArgumentException | NullPointerException e) {
598+
return defaultValue;
599+
}
600+
}
601+
602+
534603
/**
535604
* Get the optional BigInteger value associated with an index. The
536605
* defaultValue is returned if there is no value for the index, or if the

JSONObject.java

+69-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ of this software and associated documentation files (the "Software"), to deal
9292
* </ul>
9393
*
9494
* @author JSON.org
95-
* @version 2015-07-06
95+
* @version 2015-07-22
9696
*/
9797
public class JSONObject {
9898
/**
@@ -479,6 +479,31 @@ public Object get(String key) throws JSONException {
479479
return object;
480480
}
481481

482+
/**
483+
* Get the enum value associated with a key.
484+
*
485+
* @param clazz
486+
* The type of enum to retrieve.
487+
* @param key
488+
* A key string.
489+
* @return The enum value associated with the key
490+
* @throws JSONException
491+
* if the key is not found or if the value cannot be converted
492+
* to an enum.
493+
*/
494+
public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONException {
495+
E val = optEnum(clazz, key);
496+
if(val==null) {
497+
// JSONException should really take a throwable argument.
498+
// If it did, I would re-implement this with the Enum.valueOf
499+
// method and place any thrown exception in the JSONException
500+
throw new JSONException("JSONObject[" + quote(key)
501+
+ "] is not an enum of type " + quote(clazz.getSimpleName())
502+
+ ".");
503+
}
504+
return val;
505+
}
506+
482507
/**
483508
* Get the boolean value associated with a key.
484509
*
@@ -844,6 +869,49 @@ public Object opt(String key) {
844869
return key == null ? null : this.map.get(key);
845870
}
846871

872+
/**
873+
* Get the enum value associated with a key.
874+
*
875+
* @param clazz
876+
* The type of enum to retrieve.
877+
* @param key
878+
* A key string.
879+
* @return The enum value associated with the key or null if not found
880+
*/
881+
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key) {
882+
return this.optEnum(clazz, key, null);
883+
}
884+
885+
/**
886+
* Get the enum value associated with a key.
887+
*
888+
* @param clazz
889+
* The type of enum to retrieve.
890+
* @param key
891+
* A key string.
892+
* @param defaultValue
893+
* The default in case the value is not found
894+
* @return The enum value associated with the key or defaultValue
895+
* if the value is not found or cannot be assigned to clazz
896+
*/
897+
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue) {
898+
try {
899+
Object val = this.opt(key);
900+
if (NULL.equals(val)) {
901+
return defaultValue;
902+
}
903+
if (clazz.isAssignableFrom(val.getClass())) {
904+
// we just checked it!
905+
@SuppressWarnings("unchecked")
906+
E myE = (E) val;
907+
return myE;
908+
}
909+
return Enum.valueOf(clazz, val.toString());
910+
} catch (IllegalArgumentException | NullPointerException e) {
911+
return defaultValue;
912+
}
913+
}
914+
847915
/**
848916
* Get an optional boolean associated with a key. It returns false if there
849917
* is no such key, or if the value is not Boolean.TRUE or the String "true".

0 commit comments

Comments
 (0)