1111 * These tests explore how enum serialization works with JSON-Java.
1212 */
1313public class EnumTest {
14+
15+ /**
16+ * To serialize an enum by its getters, use the JSONObject Object constructor.
17+ * The JSONObject ctor handles enum like any other bean. A JSONobject
18+ * is created whose entries are the getter name/value pairs.
19+ */
1420 @ Test
1521 public void jsonObjectFromEnum () {
16- /**
17- * To serialize an enum by its getters, use the JSONObject Object constructor.
18- * The JSONObject ctor handles enum like any other bean. A JSONobject
19- * is created whose entries are the getter name/value pairs.
20- */
21-
2222 // If there are no getters then the object is empty.
2323 MyEnum myEnum = MyEnum .VAL2 ;
2424 JSONObject jsonObject = new JSONObject (myEnum );
@@ -44,12 +44,12 @@ public void jsonObjectFromEnum() {
4444 Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
4545 }
4646
47+ /**
48+ * To serialize an enum by its set of allowed values, use getNames()
49+ * and the the JSONObject Object with names constructor.
50+ */
4751 @ Test
4852 public void jsonObjectFromEnumWithNames () {
49- /**
50- * To serialize an enum by its set of allowed values, use getNames()
51- * and the the JSONObject Object with names constructor.
52- */
5353 String [] names ;
5454 String expectedStr ;
5555 JSONObject jsonObject ;
@@ -74,12 +74,13 @@ public void jsonObjectFromEnumWithNames() {
7474 expectedJsonObject = new JSONObject (expectedStr );
7575 Util .compareActualVsExpectedJsonObjects (finalJsonObject , expectedJsonObject );
7676 }
77+
78+ /**
79+ * To serialize by assigned value, use the put() methods. The value
80+ * will be stored as a enum type.
81+ */
7782 @ Test
7883 public void enumPut () {
79- /**
80- * To serialize by assigned value, use the put() methods. The value
81- * will be stored as a enum type.
82- */
8384 String expectedFinalStr = "{\" myEnum\" :\" VAL2\" , \" myEnumField\" :\" VAL1\" }" ;
8485 JSONObject jsonObject = new JSONObject ();
8586 MyEnum myEnum = MyEnum .VAL2 ;
@@ -106,12 +107,12 @@ public void enumPut() {
106107 assertTrue ("expecting myEnumField value" , MyEnumField .VAL1 .equals (jsonArray .remove (1 )));
107108 }
108109
110+ /**
111+ * The default action of valueToString() is to call object.toString().
112+ * For enums, this means the assigned value will be returned as a string.
113+ */
109114 @ Test
110115 public void enumValueToString () {
111- /**
112- * The default action of valueToString() is to call object.toString().
113- * For enums, this means the assigned value will be returned as a string.
114- */
115116 String expectedStr1 = "\" VAL1\" " ;
116117 String expectedStr2 = "\" VAL1\" " ;
117118 MyEnum myEnum = MyEnum .VAL1 ;
@@ -137,12 +138,12 @@ public void enumValueToString() {
137138 str3 .startsWith (expectedStr3 ));
138139 }
139140
141+ /**
142+ * In whatever form the enum was added to the JSONObject or JSONArray,
143+ * json[Object|Array].toString should serialize it in a reasonable way.
144+ */
140145 @ Test
141146 public void enumToString () {
142- /**
143- * In whatever form the enum was added to the JSONObject or JSONArray,
144- * json[Object|Array].toString should serialize it in a reasonable way.
145- */
146147 MyEnum myEnum = MyEnum .VAL2 ;
147148 JSONObject jsonObject = new JSONObject (myEnum );
148149 String expectedStr = "{}" ;
@@ -195,12 +196,12 @@ public void enumToString() {
195196 Util .compareActualVsExpectedJsonArrays (actualJsonArray , expectedJsonArray );
196197 }
197198
199+ /**
200+ * Wrap should handle enums exactly the same way as the JSONObject(Object)
201+ * constructor.
202+ */
198203 @ Test
199204 public void wrap () {
200- /**
201- * Wrap should handle enums exactly the same way as the JSONObject(Object)
202- * constructor.
203- */
204205 MyEnum myEnum = MyEnum .VAL2 ;
205206 JSONObject jsonObject = (JSONObject )JSONObject .wrap (myEnum );
206207 assertTrue ("simple enum has no getters" , jsonObject .length () == 0 );
@@ -220,15 +221,24 @@ public void wrap() {
220221 Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
221222 }
222223
224+ /**
225+ * It was determined that some API methods should be added to
226+ * support enums:<br>
227+ * JSONObject.getEnum(class, key)<br>
228+ * JSONObject.optEnum(class, key)<br>
229+ * JSONObject.optEnum(class, key, default)<br>
230+ * JSONArray.getEnum(class, index)<br>
231+ * JSONArray.optEnum(class, index)<br>
232+ * JSONArray.optEnum(class, index, default)<br>
233+ * <p>
234+ * Exercise these enum API methods on JSONObject and JSONArray
235+ */
223236 @ Test
224237 public void enumAPI () {
225238 MyEnumClass myEnumClass = new MyEnumClass ();
226239 myEnumClass .setMyEnum (MyEnum .VAL1 );
227240 MyEnumField myEnumField = MyEnumField .VAL2 ;
228241
229- /**
230- * Exercise the proposed enum API methods on JSONObject
231- */
232242 JSONObject jsonObject = new JSONObject ();
233243 jsonObject .put ("strKey" , "value" );
234244 jsonObject .put ("enumKey" , myEnumField );
0 commit comments