1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

.getString(...) now throws if there is no property or if it is not a string

This commit is contained in:
Douglas Crockford 2011-05-09 10:08:10 -07:00
parent dced076cd4
commit f4cb14728f
3 changed files with 32 additions and 17 deletions

View file

@ -78,7 +78,7 @@ import java.util.Map;
* </ul> * </ul>
* @author JSON.org * @author JSON.org
* @version 2010-12-28 * @version 2011-05-04
*/ */
public class JSONArray { public class JSONArray {
@ -319,11 +319,14 @@ public class JSONArray {
* Get the string associated with an index. * Get the string associated with an index.
* @param index The index must be between 0 and length() - 1. * @param index The index must be between 0 and length() - 1.
* @return A string value. * @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 { public String getString(int index) throws JSONException {
Object object = get(index); 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 * 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 index The subscript.
* @param value The Map value. * @param value The Map value.
* @return this. * @return this.

View file

@ -86,7 +86,7 @@ import java.util.ResourceBundle;
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li> * <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
* </ul> * </ul>
* @author JSON.org * @author JSON.org
* @version 2011-01-31 * @version 2011-04-05
*/ */
public class JSONObject { public class JSONObject {
@ -367,8 +367,10 @@ public class JSONObject {
* @throws JSONException If the value is an invalid number * @throws JSONException If the value is an invalid number
* or if the key is null. * or if the key is null.
*/ */
public JSONObject accumulate(String key, Object value) public JSONObject accumulate(
throws JSONException { String key,
Object value
) throws JSONException {
testValidity(value); testValidity(value);
Object object = opt(key); Object object = opt(key);
if (object == null) { if (object == null) {
@ -627,11 +629,15 @@ public class JSONObject {
* *
* @param key A key string. * @param key A key string.
* @return A string which is the value. * @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 { public String getString(String key) throws JSONException {
Object object = get(key); 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 <code>}</code>&nbsp;<small>(right brace)</small>. * with <code>}</code>&nbsp;<small>(right brace)</small>.
* @throws JSONException If the object contains an invalid number. * @throws JSONException If the object contains an invalid number.
*/ */
static String valueToString(Object value, int indentFactor, int indent) static String valueToString(
throws JSONException { Object value,
int indentFactor,
int indent
) throws JSONException {
if (value == null || value.equals(null)) { if (value == null || value.equals(null)) {
return "null"; return "null";
} }
@ -1566,10 +1575,13 @@ public class JSONObject {
return new JSONObject((Map)object); return new JSONObject((Map)object);
} }
Package objectPackage = object.getClass().getPackage(); Package objectPackage = object.getClass().getPackage();
String objectPackageName = ( objectPackage != null ? objectPackage.getName() : "" ); String objectPackageName = objectPackage != null ?
if (objectPackageName.startsWith("java.") || objectPackage.getName() : "";
objectPackageName.startsWith("javax.") || if (
object.getClass().getClassLoader() == null) { objectPackageName.startsWith("java.") ||
objectPackageName.startsWith("javax.") ||
object.getClass().getClassLoader() == null
) {
return object.toString(); return object.toString();
} }
return new JSONObject(object); return new JSONObject(object);

View file

@ -78,7 +78,7 @@ public class Test extends TestCase {
jsonobject = new JSONObject("{\"message\":null}"); jsonobject = new JSONObject("{\"message\":null}");
assertTrue(jsonobject.isNull("message")); assertTrue(jsonobject.isNull("message"));
assertEquals(null, jsonobject.getString("message")); assertEquals(null, jsonobject.get("message"));
} }
public void testJSON() throws Exception { public void testJSON() throws Exception {
@ -294,7 +294,7 @@ public class Test extends TestCase {
XML.toString(jsonobject)); XML.toString(jsonobject));
assertEquals(98.6d, jsonobject.getDouble("String"), eps); assertEquals(98.6d, jsonobject.getDouble("String"), eps);
assertTrue(jsonobject.getBoolean("bool")); assertTrue(jsonobject.getBoolean("bool"));
assertEquals(null, jsonobject.getString("to")); assertEquals(null, jsonobject.get("to"));
assertEquals("true", jsonobject.getString("true")); 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 <fine>.\",true,false,[],{}]", 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 <fine>.\",true,false,[],{}]",
jsonobject.getJSONArray("foo").toString()); jsonobject.getJSONArray("foo").toString());