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:
parent
dced076cd4
commit
f4cb14728f
3 changed files with 32 additions and 17 deletions
|
@ -78,7 +78,7 @@ import java.util.Map;
|
|||
* </ul>
|
||||
|
||||
* @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.
|
||||
|
|
|
@ -86,7 +86,7 @@ import java.util.ResourceBundle;
|
|||
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
|
||||
* </ul>
|
||||
* @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 <code>}</code> <small>(right brace)</small>.
|
||||
* @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);
|
||||
|
|
|
@ -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 <fine>.\",true,false,[],{}]",
|
||||
jsonobject.getJSONArray("foo").toString());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue