@@ -193,8 +193,10 @@ public void jsonObjectValues() {
193193 "}" ;
194194 JSONObject jsonObject = new JSONObject (str );
195195 assertTrue ("trueKey should be true" , jsonObject .getBoolean ("trueKey" ));
196+ assertTrue ("opt trueKey should be true" , jsonObject .optBoolean ("trueKey" ));
196197 assertTrue ("falseKey should be false" , !jsonObject .getBoolean ("falseKey" ));
197198 assertTrue ("trueStrKey should be true" , jsonObject .getBoolean ("trueStrKey" ));
199+ assertTrue ("trueStrKey should be true" , jsonObject .optBoolean ("trueStrKey" ));
198200 assertTrue ("falseStrKey should be false" , !jsonObject .getBoolean ("falseStrKey" ));
199201 assertTrue ("doubleKey should be double" ,
200202 jsonObject .getDouble ("doubleKey" ) == -23.45e7 );
@@ -208,6 +210,12 @@ public void jsonObjectValues() {
208210 jsonObject .getLong ("longKey" ) == 1234567890123456789L );
209211 assertTrue ("longStrKey should be long" ,
210212 jsonObject .getLong ("longStrKey" ) == 987654321098765432L );
213+ assertTrue ("xKey should not exist" ,
214+ jsonObject .isNull ("xKey" ));
215+ assertTrue ("stringKey should exist" ,
216+ jsonObject .has ("stringKey" ));
217+ assertTrue ("stringKey should string" ,
218+ jsonObject .getString ("stringKey" ).equals ("hello world!" ));
211219 JSONArray jsonArray = jsonObject .getJSONArray ("arrayKey" );
212220 assertTrue ("arrayKey should be JSONArray" ,
213221 jsonArray .getInt (0 ) == 0 &&
@@ -232,6 +240,32 @@ public void jsonObjectNames() {
232240 Util .compareActualVsExpectedStringArrays (names , expectedNames );
233241 }
234242
243+ @ Test
244+ public void jsonObjectNamesToJsonAray () {
245+ String str =
246+ "{" +
247+ "\" trueKey\" :true," +
248+ "\" falseKey\" :false," +
249+ "\" stringKey\" :\" hello world!\" ," +
250+ "}" ;
251+ String [] expectedNames = {"trueKey" , "falseKey" , "stringKey" };
252+
253+ JSONObject jsonObject = new JSONObject (str );
254+ JSONArray jsonArray = jsonObject .names ();
255+ /**
256+ * Cannot really compare to an expected JSONArray because the ordering
257+ * of the JSONObject keys is not fixed, and JSONArray comparisons
258+ * presume fixed. Since this test is limited to key strings, a
259+ * string comparison will have to suffice.
260+ */
261+ String namesStr = jsonArray .toString ();
262+ // remove square brackets, commas, and spaces
263+ namesStr = namesStr .replaceAll ("[\\ ]|\\ [|\" ]" , "" );
264+ String [] names = namesStr .split ("," );
265+
266+ Util .compareActualVsExpectedStringArrays (names , expectedNames );
267+ }
268+
235269 @ Test
236270 public void objectNames () {
237271 MyBean myBean = new MyBean ();
@@ -245,29 +279,53 @@ public void objectNames() {
245279 public void jsonObjectIncrement () {
246280 String str =
247281 "{" +
248- "\" keyLong\" :1L ," +
282+ "\" keyLong\" :9999999991 ," +
249283 "\" keyDouble\" :1.1," +
250- "\" keyFloat\" :1.1F," +
251284 "}" ;
252285 String expectedStr =
253286 "{" +
254287 "\" keyInt\" :3," +
255- "\" keyLong\" :3 ," +
288+ "\" keyLong\" :9999999993 ," +
256289 "\" keyDouble\" :3.1," +
257- "\" keyFloat\" :3.1" +
258290 "}" ;
259291 JSONObject jsonObject = new JSONObject (str );
260292 jsonObject .increment ("keyInt" );
261293 jsonObject .increment ("keyInt" );
262294 jsonObject .increment ("keyLong" );
263295 jsonObject .increment ("keyDouble" );
264- jsonObject .increment ("keyFloat" );
265296 jsonObject .increment ("keyInt" );
266297 jsonObject .increment ("keyLong" );
267298 jsonObject .increment ("keyDouble" );
268- jsonObject .increment ("keyFloat" );
269299 JSONObject expectedJsonObject = new JSONObject (expectedStr );
270300 Util .compareActualVsExpectedJsonObjects (jsonObject , expectedJsonObject );
271301 }
272302
303+ @ Test
304+ public void jsonObjectNamesToArray () {
305+ String str =
306+ "{" +
307+ "\" trueKey\" :true," +
308+ "\" falseKey\" :false," +
309+ "\" stringKey\" :\" hello world!\" ," +
310+ "}" ;
311+ String [] expectedNames = {"trueKey" , "falseKey" , "stringKey" };
312+ JSONObject jsonObject = new JSONObject (str );
313+ String [] names = JSONObject .getNames (jsonObject );
314+ Util .compareActualVsExpectedStringArrays (names , expectedNames );
315+ }
316+
317+ @ Test
318+ public void jsonObjectNumberToString () {
319+ String str ;
320+ Double dVal ;
321+ Integer iVal = 1 ;
322+ str = JSONObject .numberToString (iVal );
323+ assertTrue ("expected " +iVal +" actual " +str , iVal .toString ().equals (str ));
324+ dVal = 12.34 ;
325+ str = JSONObject .numberToString (dVal );
326+ assertTrue ("expected " +dVal +" actual " +str , dVal .toString ().equals (str ));
327+ dVal = 12.34e27 ;
328+ str = JSONObject .numberToString (dVal );
329+ assertTrue ("expected " +dVal +" actual " +str , dVal .toString ().equals (str ));
330+ }
273331}
0 commit comments