From 8f64f51f93d77871e06a2daf5a21a137c88644b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ramos=20G=C3=B3mez?= Date: Sun, 1 Jul 2012 20:48:12 -0600 Subject: [PATCH 1/2] Add support for enum as property value Add support for enum as object properties. Enums have a working toString() that represents them well so I think they should be treated like any other simple object. --- JSONObject.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index f41582cc5..a5eaccf50 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -1450,7 +1450,7 @@ public static Object wrap(Object object) { object instanceof Short || object instanceof Integer || object instanceof Long || object instanceof Boolean || object instanceof Float || object instanceof Double || - object instanceof String) { + object instanceof String || object.getClass().isEnum()) { return object; } From 6fadec437a7b6cbb3f76b146a0b3de5a3dbc8201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ramos=20G=C3=B3mez?= Date: Tue, 3 Jul 2012 02:44:37 -0600 Subject: [PATCH 2/2] Added tests using enums as bean property. --- tests/TestJSONObject.java | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/TestJSONObject.java b/tests/TestJSONObject.java index 5aad8609c..7f11bce03 100644 --- a/tests/TestJSONObject.java +++ b/tests/TestJSONObject.java @@ -250,6 +250,39 @@ public class ObjectWithPrimativesExtension extends ObjectWithPrimatives // Same Object } + /** + * Status Enum + */ + public enum Status + { + INACTIVE, + ACTIVE + } + + /** + * A class with enums as property + */ + public class ObjectWithEnumProperty + { + Status status; + + /** + * Creates an object with active status + */ + ObjectWithEnumProperty() + { + this.status = Status.ACTIVE; + } + + /** + * Gets the status. + */ + public Status getStatus() + { + return this.status; + } + } + /** The jsonobject. */ JSONObject jsonobject = new JSONObject(); @@ -824,6 +857,15 @@ public void testConstructor_ObjectWithStringArray() }).toString()); } + /** + * Tests the constructor method using object with enum properties. + */ + public void testConstructor_ObjectWithEnumProperty() + { + assertEquals("{\"status\":\"ACTIVE\"}", + new JSONObject(new ObjectWithEnumProperty()).toString()); + } + /** * Tests the opt method. */ @@ -1378,6 +1420,7 @@ public void testWrap() assertEquals(56456456L, JSONObject.wrap(56456456L)); assertEquals(JSONObject.NULL, JSONObject.wrap(null)); assertEquals(JSONObject.NULL, JSONObject.wrap(JSONObject.NULL)); + assertEquals(Status.ACTIVE, JSONObject.wrap(Status.ACTIVE)); BadJsonString a = new BadJsonString(); assertEquals(a, JSONObject.wrap(a)); NullJsonString q = new NullJsonString();