From 91107e3e823c0e8ac5ae9ea8a018b3eb421792f0 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 11 Aug 2016 12:22:31 -0400 Subject: [PATCH] Adds support to JSONObject wrap and write methods to explicitly handle Enums. The new way enums are handled is to always place the actual enum in the JSONObject/JSONArray. When writing, we always write the actual "name" of the enum, so even with a toString override on the enum class, the value remains consistant and compatible with the optEnum/getEnum methods. The constructor JSONObject(Object) functions the same way as before when passing an enum and is consistent with other "value" types. For example, when creating a JSONObject with Long, Boolean, BigDecimal as the constructor parameter, the value will be treated as a "bean". --- JSONObject.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index aa227ff..de42faa 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -1708,6 +1708,9 @@ public class JSONObject { if (value.getClass().isArray()) { return new JSONArray(value).toString(); } + if(value instanceof Enum){ + return quote(((Enum)value).name()); + } return quote(value.toString()); } @@ -1730,12 +1733,9 @@ public class JSONObject { } if (object instanceof JSONObject || object instanceof JSONArray || NULL.equals(object) || object instanceof JSONString - || object instanceof Byte || object instanceof Character - || object instanceof Short || object instanceof Integer - || object instanceof Long || object instanceof Boolean - || object instanceof Float || object instanceof Double - || object instanceof String || object instanceof BigInteger - || object instanceof BigDecimal) { + || object instanceof Number || object instanceof Character + || object instanceof Boolean || object instanceof String + || object instanceof Enum) { return object; } @@ -1797,6 +1797,8 @@ public class JSONObject { writer.write(numberToString((Number) value)); } else if (value instanceof Boolean) { writer.write(value.toString()); + } else if (value instanceof Enum) { + writer.write(quote(((Enum)value).name())); } else if (value instanceof JSONString) { Object o; try {