diff --git a/JSONArray.java b/JSONArray.java index d3499e0..6cfc8e7 100755 --- a/JSONArray.java +++ b/JSONArray.java @@ -78,7 +78,7 @@ import java.util.Map; * * @author JSON.org - * @version 2010-12-28 + * @version 2011-05-04 */ public class JSONArray { @@ -319,11 +319,14 @@ public class JSONArray { * Get the string associated with an index. * @param index The index must be between 0 and length() - 1. * @return A string value. - * @throws JSONException If there is no value for the index. + * @throws JSONException If there is no string value for the index. */ public String getString(int index) throws JSONException { Object object = get(index); - return object == JSONObject.NULL ? null : object.toString(); + if (object instanceof String) { + return (String)object; + } + throw new JSONException("JSONArray[" + index + "] not a string."); } @@ -722,7 +725,7 @@ public class JSONArray { /** * Put a value in the JSONArray, where the value will be a - * JSONObject which is produced from a Map. + * JSONObject that is produced from a Map. * @param index The subscript. * @param value The Map value. * @return this. diff --git a/JSONObject.java b/JSONObject.java index 268a726..a2b3edf 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -86,7 +86,7 @@ import java.util.ResourceBundle; *
  • Numbers may have the 0x- (hex) prefix.
  • * * @author JSON.org - * @version 2011-01-31 + * @version 2011-04-05 */ public class JSONObject { @@ -367,8 +367,10 @@ public class JSONObject { * @throws JSONException If the value is an invalid number * or if the key is null. */ - public JSONObject accumulate(String key, Object value) - throws JSONException { + public JSONObject accumulate( + String key, + Object value + ) throws JSONException { testValidity(value); Object object = opt(key); if (object == null) { @@ -627,11 +629,15 @@ public class JSONObject { * * @param key A key string. * @return A string which is the value. - * @throws JSONException if the key is not found. + * @throws JSONException if there is no string value for the key. */ public String getString(String key) throws JSONException { Object object = get(key); - return object == NULL ? null : object.toString(); + if (object instanceof String) { + return (String)object; + } + throw new JSONException("JSONObject[" + quote(key) + + "] not a string."); } @@ -1490,8 +1496,11 @@ public class JSONObject { * with } (right brace). * @throws JSONException If the object contains an invalid number. */ - static String valueToString(Object value, int indentFactor, int indent) - throws JSONException { + static String valueToString( + Object value, + int indentFactor, + int indent + ) throws JSONException { if (value == null || value.equals(null)) { return "null"; } @@ -1566,10 +1575,13 @@ public class JSONObject { return new JSONObject((Map)object); } Package objectPackage = object.getClass().getPackage(); - String objectPackageName = ( objectPackage != null ? objectPackage.getName() : "" ); - if (objectPackageName.startsWith("java.") || - objectPackageName.startsWith("javax.") || - object.getClass().getClassLoader() == null) { + String objectPackageName = objectPackage != null ? + objectPackage.getName() : ""; + if ( + objectPackageName.startsWith("java.") || + objectPackageName.startsWith("javax.") || + object.getClass().getClassLoader() == null + ) { return object.toString(); } return new JSONObject(object); diff --git a/Test.java b/Test.java index 74ce636..7f34eba 100755 --- a/Test.java +++ b/Test.java @@ -78,7 +78,7 @@ public class Test extends TestCase { jsonobject = new JSONObject("{\"message\":null}"); assertTrue(jsonobject.isNull("message")); - assertEquals(null, jsonobject.getString("message")); + assertEquals(null, jsonobject.get("message")); } public void testJSON() throws Exception { @@ -294,7 +294,7 @@ public class Test extends TestCase { XML.toString(jsonobject)); assertEquals(98.6d, jsonobject.getDouble("String"), eps); assertTrue(jsonobject.getBoolean("bool")); - assertEquals(null, jsonobject.getString("to")); + assertEquals(null, jsonobject.get("to")); assertEquals("true", jsonobject.getString("true")); assertEquals("[true,false,9876543210,0,1.00000001,1.000000000001,1,1.0E-17,2,0.1,2.0E100,-32,[],{},\"string\",666,2001.99,\"so \\\"fine\\\".\",\"so .\",true,false,[],{}]", jsonobject.getJSONArray("foo").toString());