Skip to content

Commit f4cb147

Browse files
.getString(...) now throws if there is no property or if it is not a string
1 parent dced076 commit f4cb147

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

JSONArray.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ of this software and associated documentation files (the "Software"), to deal
7878
* </ul>
7979
8080
* @author JSON.org
81-
* @version 2010-12-28
81+
* @version 2011-05-04
8282
*/
8383
public class JSONArray {
8484

@@ -319,11 +319,14 @@ public long getLong(int index) throws JSONException {
319319
* Get the string associated with an index.
320320
* @param index The index must be between 0 and length() - 1.
321321
* @return A string value.
322-
* @throws JSONException If there is no value for the index.
322+
* @throws JSONException If there is no string value for the index.
323323
*/
324324
public String getString(int index) throws JSONException {
325325
Object object = get(index);
326-
return object == JSONObject.NULL ? null : object.toString();
326+
if (object instanceof String) {
327+
return (String)object;
328+
}
329+
throw new JSONException("JSONArray[" + index + "] not a string.");
327330
}
328331

329332

@@ -722,7 +725,7 @@ public JSONArray put(int index, long value) throws JSONException {
722725

723726
/**
724727
* Put a value in the JSONArray, where the value will be a
725-
* JSONObject which is produced from a Map.
728+
* JSONObject that is produced from a Map.
726729
* @param index The subscript.
727730
* @param value The Map value.
728731
* @return this.

JSONObject.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
9191
public 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>&nbsp;<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);

Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void testNull() throws Exception {
7878

7979
jsonobject = new JSONObject("{\"message\":null}");
8080
assertTrue(jsonobject.isNull("message"));
81-
assertEquals(null, jsonobject.getString("message"));
81+
assertEquals(null, jsonobject.get("message"));
8282
}
8383

8484
public void testJSON() throws Exception {
@@ -294,7 +294,7 @@ public void testJSON() throws Exception {
294294
XML.toString(jsonobject));
295295
assertEquals(98.6d, jsonobject.getDouble("String"), eps);
296296
assertTrue(jsonobject.getBoolean("bool"));
297-
assertEquals(null, jsonobject.getString("to"));
297+
assertEquals(null, jsonobject.get("to"));
298298
assertEquals("true", jsonobject.getString("true"));
299299
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,[],{}]",
300300
jsonobject.getJSONArray("foo").toString());

0 commit comments

Comments
 (0)