|
6 | 6 | import org.junit.*; |
7 | 7 |
|
8 | 8 | /** |
9 | | - * Documents how enum is handled by JSON-Java. |
| 9 | + * Enums are not explicitly supported in JSON-Java. But because enums act like |
| 10 | + * classes, all required behavior is already be present in some form. |
| 11 | + * These tests explore how enum serialization works with JSON-Java. |
10 | 12 | */ |
11 | 13 | public class EnumTest { |
12 | | - |
13 | 14 | @Test |
14 | | - public void simpleEnum() { |
| 15 | + public void jsonObjectFromEnum() { |
15 | 16 | /** |
16 | | - * Nothing happens when a simple enum is parsed to JSONObject |
| 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. |
17 | 20 | */ |
| 21 | + |
| 22 | + // If there are no getters then the object is empty. |
18 | 23 | MyEnum myEnum = MyEnum.VAL2; |
19 | 24 | JSONObject jsonObject = new JSONObject(myEnum); |
20 | | - assertTrue("simple enum is not processed by JSONObject", jsonObject.length() == 0); |
21 | | - /** |
22 | | - * Nothing good happens when a simple enum is parsed to JSONArray |
23 | | - */ |
24 | | - try { |
25 | | - new JSONArray(myEnum); |
26 | | - } catch (JSONException e) { |
27 | | - assertTrue("JSONArray throws exception when passed enum", true); |
28 | | - } |
29 | | - } |
| 25 | + assertTrue("simple enum has no getters", jsonObject.length() == 0); |
30 | 26 |
|
31 | | - @Test |
32 | | - public void enumWithField() { |
33 | | - /** |
34 | | - * enum with a getters is handled like a bean |
35 | | - */ |
| 27 | + // enum with a getters should create a non-empty object |
36 | 28 | String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; |
37 | | - MyEnumField myEnum = MyEnumField.VAL2; |
38 | | - JSONObject jsonObject = new JSONObject(myEnum); |
| 29 | + MyEnumField myEnumField = MyEnumField.VAL2; |
| 30 | + jsonObject = new JSONObject(myEnumField); |
39 | 31 | JSONObject expectedJsonObject = new JSONObject(expectedStr); |
40 | 32 | Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); |
41 | | - } |
42 | 33 |
|
43 | | - @Test |
44 | | - public void enumInClass() { |
45 | 34 | /** |
46 | | - * class which contains enum instances. |
47 | | - * The enum values in MyEnum are lost. |
48 | | - * The string values in MyEnumFild are extracted and wrapped. |
| 35 | + * class which contains enum instances. Each enum should be stored |
| 36 | + * in its own JSONObject |
49 | 37 | */ |
50 | | - String expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; |
| 38 | + expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; |
51 | 39 | MyEnumClass myEnumClass = new MyEnumClass(); |
52 | 40 | myEnumClass.setMyEnum(MyEnum.VAL1); |
53 | 41 | myEnumClass.setMyEnumField(MyEnumField.VAL3); |
54 | | - JSONObject jsonObject = new JSONObject(myEnumClass); |
55 | | - JSONObject expectedJsonObject = new JSONObject(expectedStr); |
| 42 | + jsonObject = new JSONObject(myEnumClass); |
| 43 | + expectedJsonObject = new JSONObject(expectedStr); |
56 | 44 | Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); |
57 | 45 | } |
58 | 46 |
|
| 47 | + @Test |
| 48 | + 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 | + */ |
| 53 | + String [] names; |
| 54 | + String expectedStr; |
| 55 | + JSONObject jsonObject; |
| 56 | + JSONObject finalJsonObject; |
| 57 | + JSONObject expectedJsonObject; |
| 58 | + |
| 59 | + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; |
| 60 | + MyEnum myEnum = MyEnum.VAL1; |
| 61 | + names = JSONObject.getNames(myEnum); |
| 62 | + // The values will be MyEnmField fields, so need to convert back to string for comparison |
| 63 | + jsonObject = new JSONObject(myEnum, names); |
| 64 | + finalJsonObject = new JSONObject(jsonObject.toString()); |
| 65 | + expectedJsonObject = new JSONObject(expectedStr); |
| 66 | + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); |
| 67 | + |
| 68 | + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; |
| 69 | + MyEnumField myEnumField = MyEnumField.VAL3; |
| 70 | + names = JSONObject.getNames(myEnumField); |
| 71 | + // The values will be MyEnmField fields, so need to convert back to string for comparison |
| 72 | + jsonObject = new JSONObject(myEnumField, names); |
| 73 | + finalJsonObject = new JSONObject(jsonObject.toString()); |
| 74 | + expectedJsonObject = new JSONObject(expectedStr); |
| 75 | + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject); |
| 76 | + } |
| 77 | + @Test |
| 78 | + 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 | + */ |
| 83 | + String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}"; |
| 84 | + JSONObject jsonObject = new JSONObject(); |
| 85 | + MyEnum myEnum = MyEnum.VAL2; |
| 86 | + jsonObject.put("myEnum", myEnum); |
| 87 | + assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.get("myEnum"))); |
| 88 | + assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonObject.opt("myEnum"))); |
| 89 | + MyEnumField myEnumField = MyEnumField.VAL1; |
| 90 | + jsonObject.putOnce("myEnumField", myEnumField); |
| 91 | + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.get("myEnumField"))); |
| 92 | + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonObject.opt("myEnumField"))); |
| 93 | + JSONObject finalJsonObject = new JSONObject(jsonObject.toString()); |
| 94 | + JSONObject expectedFinalJsonObject = new JSONObject(expectedFinalStr); |
| 95 | + Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedFinalJsonObject); |
| 96 | + |
| 97 | + JSONArray jsonArray = new JSONArray(); |
| 98 | + jsonArray.put(myEnum); |
| 99 | + jsonArray.put(1, myEnumField); |
| 100 | + assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0))); |
| 101 | + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1))); |
| 102 | + JSONArray expectedJsonArray = new JSONArray(); |
| 103 | + expectedJsonArray.put(MyEnum.VAL2); |
| 104 | + expectedJsonArray.put(MyEnumField.VAL1); |
| 105 | + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); |
| 106 | + assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1))); |
| 107 | + } |
| 108 | + |
59 | 109 | @Test |
60 | 110 | 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 | + */ |
61 | 115 | String expectedStr1 = "\"VAL1\""; |
62 | 116 | String expectedStr2 = "\"VAL1\""; |
63 | | - String expectedStr3 = "\"org.json.junit.MyEnumClass@"; |
64 | 117 | MyEnum myEnum = MyEnum.VAL1; |
65 | 118 | MyEnumField myEnumField = MyEnumField.VAL1; |
66 | 119 | MyEnumClass myEnumClass = new MyEnumClass(); |
67 | | - myEnumClass.setMyEnum(MyEnum.VAL1); |
68 | | - myEnumClass.setMyEnumField(MyEnumField.VAL1); |
69 | 120 |
|
70 | 121 | String str1 = JSONObject.valueToString(myEnum); |
71 | 122 | assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1, |
72 | 123 | str1.equals(expectedStr1)); |
73 | 124 | String str2 = JSONObject.valueToString(myEnumField); |
74 | 125 | assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2, |
75 | 126 | str2.equals(expectedStr2)); |
| 127 | + |
| 128 | + /** |
| 129 | + * However, an enum within another class will not be rendered |
| 130 | + * unless that class overrides default toString() |
| 131 | + */ |
| 132 | + String expectedStr3 = "\"org.json.junit.MyEnumClass@"; |
| 133 | + myEnumClass.setMyEnum(MyEnum.VAL1); |
| 134 | + myEnumClass.setMyEnumField(MyEnumField.VAL1); |
76 | 135 | String str3 = JSONObject.valueToString(myEnumClass); |
77 | 136 | assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3, |
78 | 137 | str3.startsWith(expectedStr3)); |
79 | 138 | } |
| 139 | + |
| 140 | + @Test |
| 141 | + 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 | + */ |
| 146 | + MyEnum myEnum = MyEnum.VAL2; |
| 147 | + JSONObject jsonObject = new JSONObject(myEnum); |
| 148 | + String expectedStr = "{}"; |
| 149 | + assertTrue("myEnum toString() should be empty", expectedStr.equals(jsonObject.toString())); |
| 150 | + |
| 151 | + MyEnumField myEnumField = MyEnumField.VAL2; |
| 152 | + jsonObject = new JSONObject(myEnumField); |
| 153 | + expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; |
| 154 | + JSONObject actualJsonObject = new JSONObject(jsonObject.toString()); |
| 155 | + JSONObject expectedJsonObject = new JSONObject(expectedStr); |
| 156 | + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); |
| 157 | + |
| 158 | + expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; |
| 159 | + MyEnumClass myEnumClass = new MyEnumClass(); |
| 160 | + myEnumClass.setMyEnum(MyEnum.VAL1); |
| 161 | + myEnumClass.setMyEnumField(MyEnumField.VAL3); |
| 162 | + jsonObject = new JSONObject(myEnumClass); |
| 163 | + actualJsonObject = new JSONObject(jsonObject.toString()); |
| 164 | + expectedJsonObject = new JSONObject(expectedStr); |
| 165 | + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); |
| 166 | + |
| 167 | + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; |
| 168 | + String [] names = JSONObject.getNames(myEnum); |
| 169 | + jsonObject = new JSONObject(myEnum, names); |
| 170 | + actualJsonObject = new JSONObject(jsonObject.toString()); |
| 171 | + expectedJsonObject = new JSONObject(expectedStr); |
| 172 | + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); |
| 173 | + |
| 174 | + expectedStr = "{\"VAL1\":\"VAL1\",\"VAL2\":\"VAL2\",\"VAL3\":\"VAL3\"}"; |
| 175 | + names = JSONObject.getNames(myEnumField); |
| 176 | + jsonObject = new JSONObject(myEnumField, names); |
| 177 | + actualJsonObject = new JSONObject(jsonObject.toString()); |
| 178 | + expectedJsonObject = new JSONObject(expectedStr); |
| 179 | + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); |
| 180 | + |
| 181 | + expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}"; |
| 182 | + jsonObject = new JSONObject(); |
| 183 | + jsonObject.putOpt("myEnum", myEnum); |
| 184 | + jsonObject.putOnce("myEnumField", myEnumField); |
| 185 | + actualJsonObject = new JSONObject(jsonObject.toString()); |
| 186 | + expectedJsonObject = new JSONObject(expectedStr); |
| 187 | + Util.compareActualVsExpectedJsonObjects(actualJsonObject, expectedJsonObject); |
| 188 | + |
| 189 | + expectedStr = "[\"VAL2\", \"VAL2\"]"; |
| 190 | + JSONArray jsonArray = new JSONArray(); |
| 191 | + jsonArray.put(myEnum); |
| 192 | + jsonArray.put(1, myEnumField); |
| 193 | + JSONArray actualJsonArray = new JSONArray(jsonArray.toString()); |
| 194 | + JSONArray expectedJsonArray = new JSONArray(expectedStr); |
| 195 | + Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray); |
| 196 | + } |
| 197 | + |
| 198 | + public void wrap() { |
| 199 | + /** |
| 200 | + * Wrap should handle enums exactly the same way as the JSONObject(Object) |
| 201 | + * constructor. |
| 202 | + */ |
| 203 | + MyEnum myEnum = MyEnum.VAL2; |
| 204 | + JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum); |
| 205 | + assertTrue("simple enum has no getters", jsonObject.length() == 0); |
| 206 | + |
| 207 | + String expectedStr = "{\"value\":\"val 2\", \"intVal\":2}"; |
| 208 | + MyEnumField myEnumField = MyEnumField.VAL2; |
| 209 | + jsonObject = (JSONObject)JSONObject.wrap(myEnumField); |
| 210 | + JSONObject expectedJsonObject = new JSONObject(expectedStr); |
| 211 | + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); |
| 212 | + |
| 213 | + expectedStr = "{\"myEnumField\":{\"intVal\":3,\"value\":\"val 3\"},\"myEnum\":{}}"; |
| 214 | + MyEnumClass myEnumClass = new MyEnumClass(); |
| 215 | + myEnumClass.setMyEnum(MyEnum.VAL1); |
| 216 | + myEnumClass.setMyEnumField(MyEnumField.VAL3); |
| 217 | + jsonObject = (JSONObject)JSONObject.wrap(myEnumClass); |
| 218 | + expectedJsonObject = new JSONObject(expectedStr); |
| 219 | + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); |
| 220 | + } |
80 | 221 | } |
0 commit comments