@@ -2010,6 +2010,8 @@ public void jsonObjectOptDefault() {
20102010 public void jsonObjectOptNoKey () {
20112011
20122012 JSONObject jsonObject = new JSONObject ();
2013+
2014+ assertNull (jsonObject .opt (null ));
20132015
20142016 assertTrue ("optBigDecimal() should return default BigDecimal" ,
20152017 BigDecimal .TEN .compareTo (jsonObject .optBigDecimal ("myKey" , BigDecimal .TEN ))==0 );
@@ -2088,6 +2090,46 @@ public void jsonObjectOptCoercion() {
20882090 assertEquals (19007199254740992l , (long )Double .parseDouble ("19007199254740993.35481234487103587486413587843213584" ));
20892091 assertEquals (2147483647 , (int )Double .parseDouble ("19007199254740993.35481234487103587486413587843213584" ));
20902092 }
2093+
2094+ /**
2095+ * Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently.
2096+ */
2097+ @ Test
2098+ public void jsonObjectOptBigDecimal () {
2099+ JSONObject jo = new JSONObject ().put ("int" , 123 ).put ("long" , 654L )
2100+ .put ("float" , 1.234f ).put ("double" , 2.345d )
2101+ .put ("bigInteger" , new BigInteger ("1234" ))
2102+ .put ("bigDecimal" , new BigDecimal ("1234.56789" ))
2103+ .put ("nullVal" , JSONObject .NULL );
2104+
2105+ assertEquals (new BigDecimal ("123" ),jo .optBigDecimal ("int" , null ));
2106+ assertEquals (new BigDecimal ("654" ),jo .optBigDecimal ("long" , null ));
2107+ assertEquals (new BigDecimal (1.234f ),jo .optBigDecimal ("float" , null ));
2108+ assertEquals (new BigDecimal (2.345d ),jo .optBigDecimal ("double" , null ));
2109+ assertEquals (new BigDecimal ("1234" ),jo .optBigDecimal ("bigInteger" , null ));
2110+ assertEquals (new BigDecimal ("1234.56789" ),jo .optBigDecimal ("bigDecimal" , null ));
2111+ assertNull (jo .optBigDecimal ("nullVal" , null ));
2112+ }
2113+
2114+ /**
2115+ * Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently.
2116+ */
2117+ @ Test
2118+ public void jsonObjectOptBigInteger () {
2119+ JSONObject jo = new JSONObject ().put ("int" , 123 ).put ("long" , 654L )
2120+ .put ("float" , 1.234f ).put ("double" , 2.345d )
2121+ .put ("bigInteger" , new BigInteger ("1234" ))
2122+ .put ("bigDecimal" , new BigDecimal ("1234.56789" ))
2123+ .put ("nullVal" , JSONObject .NULL );
2124+
2125+ assertEquals (new BigInteger ("123" ),jo .optBigInteger ("int" , null ));
2126+ assertEquals (new BigInteger ("654" ),jo .optBigInteger ("long" , null ));
2127+ assertEquals (new BigInteger ("1" ),jo .optBigInteger ("float" , null ));
2128+ assertEquals (new BigInteger ("2" ),jo .optBigInteger ("double" , null ));
2129+ assertEquals (new BigInteger ("1234" ),jo .optBigInteger ("bigInteger" , null ));
2130+ assertEquals (new BigInteger ("1234" ),jo .optBigInteger ("bigDecimal" , null ));
2131+ assertNull (jo .optBigDecimal ("nullVal" , null ));
2132+ }
20912133
20922134 /**
20932135 * Confirm behavior when JSONObject put(key, null object) is called
@@ -2099,13 +2141,13 @@ public void jsonObjectputNull() {
20992141 String str = "{\" myKey\" : \" myval\" }" ;
21002142 JSONObject jsonObjectRemove = new JSONObject (str );
21012143 jsonObjectRemove .remove ("myKey" );
2144+ assertEquals ("jsonObject should be empty" ,0 ,jsonObjectRemove .length ());
21022145
21032146 JSONObject jsonObjectPutNull = new JSONObject (str );
21042147 jsonObjectPutNull .put ("myKey" , (Object ) null );
2148+ assertEquals ("jsonObject should be empty" ,0 ,jsonObjectPutNull .length ());
2149+
21052150
2106- // validate JSON
2107- assertTrue ("jsonObject should be empty" , jsonObjectRemove .length () == 0
2108- && jsonObjectPutNull .length () == 0 );
21092151 }
21102152
21112153 /**
@@ -2190,6 +2232,70 @@ public void write() throws IOException {
21902232 stringWriter .close ();
21912233 }
21922234 }
2235+
2236+ /**
2237+ * Confirms that exceptions thrown when writing values are wrapped properly.
2238+ */
2239+ @ Test
2240+ public void testJSONWriterException () throws IOException {
2241+ final JSONObject jsonObject = new JSONObject ();
2242+
2243+ jsonObject .put ("someKey" ,new BrokenToString ());
2244+
2245+ // test single element JSONObject
2246+ try (StringWriter writer = new StringWriter ();) {
2247+ jsonObject .write (writer ).toString ();
2248+ fail ("Expected an exception, got a String value" );
2249+ } catch (JSONException e ) {
2250+ assertEquals ("Unable to write JSONObject value for key: someKey" , e .getMessage ());
2251+ } catch (Exception e ) {
2252+ fail ("Expected JSONException" );
2253+ }
2254+
2255+ //test multiElement
2256+ jsonObject .put ("somethingElse" , "a value" );
2257+
2258+ try (StringWriter writer = new StringWriter ()) {
2259+ jsonObject .write (writer ).toString ();
2260+ fail ("Expected an exception, got a String value" );
2261+ } catch (JSONException e ) {
2262+ assertEquals ("Unable to write JSONObject value for key: someKey" , e .getMessage ());
2263+ } catch (Exception e ) {
2264+ fail ("Expected JSONException" );
2265+ }
2266+
2267+ // test a more complex object
2268+ try (StringWriter writer = new StringWriter ()) {
2269+ new JSONObject ()
2270+ .put ("somethingElse" , "a value" )
2271+ .put ("someKey" , new JSONArray ()
2272+ .put (new JSONObject ().put ("key1" , new BrokenToString ())))
2273+ .write (writer ).toString ();
2274+ fail ("Expected an exception, got a String value" );
2275+ } catch (JSONException e ) {
2276+ assertEquals ("Unable to write JSONObject value for key: someKey" , e .getMessage ());
2277+ } catch (Exception e ) {
2278+ fail ("Expected JSONException" );
2279+ }
2280+
2281+ // test a more slightly complex object
2282+ try (StringWriter writer = new StringWriter ()) {
2283+ new JSONObject ()
2284+ .put ("somethingElse" , "a value" )
2285+ .put ("someKey" , new JSONArray ()
2286+ .put (new JSONObject ().put ("key1" , new BrokenToString ()))
2287+ .put (12345 )
2288+ )
2289+ .write (writer ).toString ();
2290+ fail ("Expected an exception, got a String value" );
2291+ } catch (JSONException e ) {
2292+ assertEquals ("Unable to write JSONObject value for key: someKey" , e .getMessage ());
2293+ } catch (Exception e ) {
2294+ fail ("Expected JSONException" );
2295+ }
2296+
2297+ }
2298+
21932299
21942300 /**
21952301 * Exercise the JSONObject write() method
@@ -2468,4 +2574,16 @@ public void toMap() {
24682574 assertTrue ("Map should have 2 elements" , map .size () == 2 );
24692575
24702576 }
2577+
2578+ /**
2579+ * test class for verifying write errors.
2580+ * @author John Aylward
2581+ *
2582+ */
2583+ private static class BrokenToString {
2584+ @ Override
2585+ public String toString () {
2586+ throw new IllegalStateException ("Something went horribly wrong!" );
2587+ }
2588+ }
24712589}
0 commit comments