@@ -86,7 +86,7 @@ of this software and associated documentation files (the "Software"), to deal
86
86
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
87
87
* </ul>
88
88
* @author JSON.org
89
- * @version 2011-01-31
89
+ * @version 2011-04-05
90
90
*/
91
91
public class JSONObject {
92
92
@@ -367,8 +367,10 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
367
367
* @throws JSONException If the value is an invalid number
368
368
* or if the key is null.
369
369
*/
370
- public JSONObject accumulate (String key , Object value )
371
- throws JSONException {
370
+ public JSONObject accumulate (
371
+ String key ,
372
+ Object value
373
+ ) throws JSONException {
372
374
testValidity (value );
373
375
Object object = opt (key );
374
376
if (object == null ) {
@@ -627,11 +629,15 @@ public static String[] getNames(Object object) {
627
629
*
628
630
* @param key A key string.
629
631
* @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 .
631
633
*/
632
634
public String getString (String key ) throws JSONException {
633
635
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." );
635
641
}
636
642
637
643
@@ -1490,8 +1496,11 @@ public static String valueToString(Object value) throws JSONException {
1490
1496
* with <code>}</code> <small>(right brace)</small>.
1491
1497
* @throws JSONException If the object contains an invalid number.
1492
1498
*/
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 {
1495
1504
if (value == null || value .equals (null )) {
1496
1505
return "null" ;
1497
1506
}
@@ -1566,10 +1575,13 @@ public static Object wrap(Object object) {
1566
1575
return new JSONObject ((Map )object );
1567
1576
}
1568
1577
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
+ ) {
1573
1585
return object .toString ();
1574
1586
}
1575
1587
return new JSONObject (object );
0 commit comments