11package org .json .junit ;
22
3- import static org .junit .Assert .* ;
3+ import static org .junit .Assert .assertTrue ;
44
5- import java .util .*;
5+ import java .util .EnumSet ;
6+ import java .util .List ;
7+ import java .util .Map ;
8+ import java .util .Set ;
69
7- import org .json .*;
8- import org .junit .*;
10+ import org .json .JSONArray ;
11+ import org .json .JSONObject ;
12+ import org .junit .Test ;
913
10- import com .jayway .jsonpath .*;
14+ import com .jayway .jsonpath .Configuration ;
15+ import com .jayway .jsonpath .JsonPath ;
1116
1217/**
1318 * Enums are not explicitly supported in JSON-Java. But because enums act like
@@ -50,11 +55,12 @@ public void jsonObjectFromEnum() {
5055
5156 // validate JSON content
5257 doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
53- assertTrue ("expecting 2 items in top level object" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
54- assertTrue ("expecting 2 items in myEnumField object" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnumField" ))).size () == 2 );
55- assertTrue ("expecting 0 items in myEnum object" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnum" ))).size () == 0 );
56- assertTrue ("expecting 3" , Integer .valueOf (3 ).equals (jsonObject .query ("/myEnumField/intVal" )));
57- assertTrue ("expecting val 3" , "val 3" .equals (jsonObject .query ("/myEnumField/value" )));
58+ assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
59+ assertTrue ("expected 2 myEnumField items" , "VAL3" .equals ((JsonPath .read (doc , "$.myEnumField" ))));
60+ assertTrue ("expected 0 myEnum items" , "VAL1" .equals ((JsonPath .read (doc , "$.myEnum" ))));
61+
62+ assertTrue ("expecting MyEnumField.VAL3" , MyEnumField .VAL3 .equals (jsonObject .query ("/myEnumField" )));
63+ assertTrue ("expecting MyEnum.VAL1" , MyEnum .VAL1 .equals (jsonObject .query ("/myEnum" )));
5864 }
5965
6066 /**
@@ -87,6 +93,45 @@ public void jsonObjectFromEnumWithNames() {
8793 assertTrue ("expected VAL1" , MyEnumField .VAL1 .equals (jsonObject .query ("/VAL1" )));
8894 assertTrue ("expected VAL2" , MyEnumField .VAL2 .equals (jsonObject .query ("/VAL2" )));
8995 assertTrue ("expected VAL3" , myEnumField .VAL3 .equals (jsonObject .query ("/VAL3" )));
96+ }
97+
98+ /**
99+ * Verify that enums are handled consistently between JSONArray and JSONObject
100+ */
101+ @ Test
102+ public void verifyEnumConsistency (){
103+ JSONObject jo = new JSONObject ();
104+
105+ jo .put ("value" , MyEnumField .VAL2 );
106+ String expected ="{\" value\" :\" VAL2\" }" ;
107+ String actual = jo .toString ();
108+ assertTrue ("Expected " +expected +" but actual was " +actual , expected .equals (actual ));
109+
110+ jo .accumulate ("value" , MyEnumField .VAL1 );
111+ expected ="{\" value\" :[\" VAL2\" ,\" VAL1\" ]}" ;
112+ actual = jo .toString ();
113+ assertTrue ("Expected " +expected +" but actual was " +actual , expected .equals (actual ));
114+
115+ jo .remove ("value" );
116+ jo .append ("value" , MyEnumField .VAL1 );
117+ expected ="{\" value\" :[\" VAL1\" ]}" ;
118+ actual = jo .toString ();
119+ assertTrue ("Expected " +expected +" but actual was " +actual , expected .equals (actual ));
120+
121+ jo .put ("value" , EnumSet .of (MyEnumField .VAL2 ));
122+ expected ="{\" value\" :[\" VAL2\" ]}" ;
123+ actual = jo .toString ();
124+ assertTrue ("Expected " +expected +" but actual was " +actual , expected .equals (actual ));
125+
126+ JSONArray ja = new JSONArray ();
127+ ja .put (MyEnumField .VAL2 );
128+ jo .put ("value" , ja );
129+ actual = jo .toString ();
130+ assertTrue ("Expected " +expected +" but actual was " +actual , expected .equals (actual ));
131+
132+ jo .put ("value" , new MyEnumField []{MyEnumField .VAL2 });
133+ actual = jo .toString ();
134+ assertTrue ("Expected " +expected +" but actual was " +actual , expected .equals (actual ));
90135
91136 }
92137
@@ -185,10 +230,8 @@ public void enumToString() {
185230 // validate JSON content
186231 doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
187232 assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
188- assertTrue ("expected 2 myEnumField items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnumField" ))).size () == 2 );
189- assertTrue ("expected 0 myEnum items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnum" ))).size () == 0 );
190- assertTrue ("expected 3" , Integer .valueOf (3 ).equals (jsonObject .query ("/myEnumField/intVal" )));
191- assertTrue ("expected val 3" , "val 3" .equals (jsonObject .query ("/myEnumField/value" )));
233+ assertTrue ("expected VAL3" , "VAL3" .equals ((JsonPath .read (doc , "$.myEnumField" ))));
234+ assertTrue ("expected VAL1" , "VAL1" .equals ((JsonPath .read (doc , "$.myEnum" ))));
192235
193236 String [] names = JSONObject .getNames (myEnum );
194237 jsonObject = new JSONObject (myEnum , names );
@@ -233,23 +276,20 @@ public void enumToString() {
233276 }
234277
235278 /**
236- * Wrap should handle enums exactly the same way as the JSONObject(Object)
237- * constructor.
279+ * Wrap should handle enums exactly as a value type like Integer, Boolean, or String.
238280 */
239281 @ Test
240282 public void wrap () {
241- MyEnum myEnum = MyEnum .VAL2 ;
242- JSONObject jsonObject = (JSONObject )JSONObject .wrap (myEnum );
243- assertTrue ("simple enum has no getters" , jsonObject .length () == 0 );
283+ assertTrue ("simple enum has no getters" , JSONObject .wrap (MyEnum .VAL2 ) instanceof MyEnum );
244284
245285 MyEnumField myEnumField = MyEnumField .VAL2 ;
246- jsonObject = (JSONObject )JSONObject .wrap (myEnumField );
286+ JSONObject jsonObject = new JSONObject ();
287+ jsonObject .put ("enum" ,myEnumField );
247288
248289 // validate JSON content
249290 Object doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
250- assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
251- assertTrue ("expected val 2" , "val 2" .equals (jsonObject .query ("/value" )));
252- assertTrue ("expected 2" , Integer .valueOf (2 ).equals (jsonObject .query ("/intVal" )));
291+ assertTrue ("expected 1 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 1 );
292+ assertTrue ("expected VAL2" , MyEnumField .VAL2 .equals (jsonObject .query ("/enum" )));
253293
254294 MyEnumClass myEnumClass = new MyEnumClass ();
255295 myEnumClass .setMyEnum (MyEnum .VAL1 );
@@ -259,11 +299,11 @@ public void wrap() {
259299 // validate JSON content
260300 doc = Configuration .defaultConfiguration ().jsonProvider ().parse (jsonObject .toString ());
261301 assertTrue ("expected 2 top level items" , ((Map <?,?>)(JsonPath .read (doc , "$" ))).size () == 2 );
262- assertTrue ("expected 2 myEnumField items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnumField" ))).size () == 2 );
263- assertTrue ("expected 0 myEnum items" , ((Map <?,?>)(JsonPath .read (doc , "$.myEnum" ))).size () == 0 );
264- assertTrue ("expected 3" , Integer .valueOf (3 ).equals (jsonObject .query ("/myEnumField/intVal" )));
265- assertTrue ("expected val 3" , "val 3" .equals (jsonObject .query ("/myEnumField/value" )));
302+ assertTrue ("expected VAL3" , "VAL3" .equals ((JsonPath .read (doc , "$.myEnumField" ))));
303+ assertTrue ("expected VAL1" , "VAL1" .equals ((JsonPath .read (doc , "$.myEnum" ))));
266304
305+ assertTrue ("expecting MyEnumField.VAL3" , MyEnumField .VAL3 .equals (jsonObject .query ("/myEnumField" )));
306+ assertTrue ("expecting MyEnum.VAL1" , MyEnum .VAL1 .equals (jsonObject .query ("/myEnum" )));
267307 }
268308
269309 /**
0 commit comments