@@ -86,7 +86,7 @@ of this software and associated documentation files (the "Software"), to deal
8686 * <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
8787 * </ul>
8888 * @author JSON.org
89- * @version 2011-01-31
89+ * @version 2011-04-05
9090 */
9191public class JSONObject {
9292
@@ -367,8 +367,10 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
367367 * @throws JSONException If the value is an invalid number
368368 * or if the key is null.
369369 */
370- public JSONObject accumulate (String key , Object value )
371- throws JSONException {
370+ public JSONObject accumulate (
371+ String key ,
372+ Object value
373+ ) throws JSONException {
372374 testValidity (value );
373375 Object object = opt (key );
374376 if (object == null ) {
@@ -627,11 +629,15 @@ public static String[] getNames(Object object) {
627629 *
628630 * @param key A key string.
629631 * @return A string which is the value.
630- * @throws JSONException if the key is not found .
632+ * @throws JSONException if there is no string value for the key .
631633 */
632634 public String getString (String key ) throws JSONException {
633635 Object object = get (key );
634- return object == NULL ? null : object .toString ();
636+ if (object instanceof String ) {
637+ return (String )object ;
638+ }
639+ throw new JSONException ("JSONObject[" + quote (key ) +
640+ "] not a string." );
635641 }
636642
637643
@@ -1490,8 +1496,11 @@ public static String valueToString(Object value) throws JSONException {
14901496 * with <code>}</code> <small>(right brace)</small>.
14911497 * @throws JSONException If the object contains an invalid number.
14921498 */
1493- static String valueToString (Object value , int indentFactor , int indent )
1494- throws JSONException {
1499+ static String valueToString (
1500+ Object value ,
1501+ int indentFactor ,
1502+ int indent
1503+ ) throws JSONException {
14951504 if (value == null || value .equals (null )) {
14961505 return "null" ;
14971506 }
@@ -1566,10 +1575,13 @@ public static Object wrap(Object object) {
15661575 return new JSONObject ((Map )object );
15671576 }
15681577 Package objectPackage = object .getClass ().getPackage ();
1569- String objectPackageName = ( objectPackage != null ? objectPackage .getName () : "" );
1570- if (objectPackageName .startsWith ("java." ) ||
1571- objectPackageName .startsWith ("javax." ) ||
1572- object .getClass ().getClassLoader () == null ) {
1578+ String objectPackageName = objectPackage != null ?
1579+ objectPackage .getName () : "" ;
1580+ if (
1581+ objectPackageName .startsWith ("java." ) ||
1582+ objectPackageName .startsWith ("javax." ) ||
1583+ object .getClass ().getClassLoader () == null
1584+ ) {
15731585 return object .toString ();
15741586 }
15751587 return new JSONObject (object );
0 commit comments