From ca3001629a398267fd312ee588cf7408bfe177ef Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 22 Jul 2015 20:11:07 -0500 Subject: [PATCH 1/3] latest --- JSONArray.java | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ JSONObject.java | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/JSONArray.java b/JSONArray.java index b1334db..5806917 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -247,6 +247,31 @@ public class JSONArray implements Iterable { } } + /** + * 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. + * @throws JSONException + * if the key is not found or if the value cannot be converted + * to an enum. + */ + public > E getEnum(Class 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. * @@ -531,6 +556,50 @@ public class JSONArray implements Iterable { } } + /** + * 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 or null if not found + */ + public > E optEnum(Class 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 or defaultValue if the value is not found or + * cannot be assigned to clazz + */ + public > E optEnum(Class 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 diff --git a/JSONObject.java b/JSONObject.java index 34d5c6c..2bf733b 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -479,6 +479,31 @@ public class JSONObject { 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. + * @throws JSONException + * if the key is not found or if the value cannot be converted + * to an enum. + */ + public > E getEnum(Class 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. * @@ -844,6 +869,49 @@ public class JSONObject { 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 or null if not found + */ + public > E optEnum(Class 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 or defaultValue if the value is not found or + * cannot be assigned to clazz + */ + public > E optEnum(Class 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". From 9785b4ff0b2d920ea81aa1303f79d5bfb1485135 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 22 Jul 2015 20:16:02 -0500 Subject: [PATCH 2/3] enum support --- JSONArray.java | 8 ++++---- JSONObject.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 5806917..3aaa185 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -254,7 +254,7 @@ public class JSONArray implements Iterable { * The type of enum to retrieve. * @param index * The index must be between 0 and length() - 1. - * @return The enum value. + * @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. @@ -563,7 +563,7 @@ public class JSONArray implements Iterable { * The type of enum to retrieve. * @param index * The index must be between 0 and length() - 1. - * @return The enum value or null if not found + * @return The enum value at the index location or null if not found */ public > E optEnum(Class clazz, int index) { return this.optEnum(clazz, index, null); @@ -578,8 +578,8 @@ public class JSONArray implements Iterable { * The index must be between 0 and length() - 1. * @param defaultValue * The default in case the value is not found - * @return The enum value or defaultValue if the value is not found or - * cannot be assigned to clazz + * @return The enum value at the index location or defaultValue if + * the value is not found or cannot be assigned to clazz */ public > E optEnum(Class clazz, int index, E defaultValue) { try { diff --git a/JSONObject.java b/JSONObject.java index 2bf733b..fc7fb81 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -486,7 +486,7 @@ public class JSONObject { * The type of enum to retrieve. * @param key * A key string. - * @return The enum value. + * @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. @@ -876,7 +876,7 @@ public class JSONObject { * The type of enum to retrieve. * @param key * A key string. - * @return The enum value or null if not found + * @return The enum value associated with the key or null if not found */ public > E optEnum(Class clazz, String key) { return this.optEnum(clazz, key, null); @@ -891,8 +891,8 @@ public class JSONObject { * A key string. * @param defaultValue * The default in case the value is not found - * @return The enum value or defaultValue if the value is not found or - * cannot be assigned to clazz + * @return The enum value associated with the key or defaultValue + * if the value is not found or cannot be assigned to clazz */ public > E optEnum(Class clazz, String key, E defaultValue) { try { From 5fc22e32a89698703cacc17952864d0f42660625 Mon Sep 17 00:00:00 2001 From: stleary Date: Wed, 22 Jul 2015 20:18:30 -0500 Subject: [PATCH 3/3] fix edit dates for enum support --- JSONArray.java | 2 +- JSONObject.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index 3aaa185..b2717d9 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -76,7 +76,7 @@ import java.util.Map; * * * @author JSON.org - * @version 2015-07-06 + * @version 2015-07-22 */ public class JSONArray implements Iterable { diff --git a/JSONObject.java b/JSONObject.java index fc7fb81..ed1159e 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -92,7 +92,7 @@ import java.util.Set; * * * @author JSON.org - * @version 2015-07-06 + * @version 2015-07-22 */ public class JSONObject { /**