Skip to content

Commit 01237c9

Browse files
committed
Missing fields will not throw an exception; return null or empty string instead
1 parent 8114b97 commit 01237c9

1 file changed

Lines changed: 3 additions & 40 deletions

File tree

‎JSONObject.java‎

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ public Object get(String key) throws JSONException {
473473
}
474474
Object object = this.opt(key);
475475
if (object == null) {
476-
throw new JSONException("JSONObject[" + quote(key) + "] not found.");
476+
return "";
477477
}
478478
return object;
479479
}
@@ -574,6 +574,8 @@ public JSONArray getJSONArray(String key) throws JSONException {
574574
*/
575575
public JSONObject getJSONObject(String key) throws JSONException {
576576
Object object = this.get(key);
577+
if (object == null)
578+
return null;
577579
if (object instanceof JSONObject) {
578580
return (JSONObject) object;
579581
}
@@ -1284,45 +1286,6 @@ public Object remove(String key) {
12841286
return this.map.remove(key);
12851287
}
12861288

1287-
/**
1288-
* Determine if two JSONObjects are similar.
1289-
* They must contain the same set of names which must be associated with
1290-
* similar values.
1291-
*
1292-
* @param other The other JSONObject
1293-
* @return true if they are equal
1294-
*/
1295-
public boolean similar(Object other) {
1296-
try {
1297-
if (!(other instanceof JSONObject)) {
1298-
return false;
1299-
}
1300-
Set<String> set = this.keySet();
1301-
if (!set.equals(((JSONObject)other).keySet())) {
1302-
return false;
1303-
}
1304-
Iterator<String> iterator = set.iterator();
1305-
while (iterator.hasNext()) {
1306-
String name = iterator.next();
1307-
Object valueThis = this.get(name);
1308-
Object valueOther = ((JSONObject)other).get(name);
1309-
if (valueThis instanceof JSONObject) {
1310-
if (!((JSONObject)valueThis).similar(valueOther)) {
1311-
return false;
1312-
}
1313-
} else if (valueThis instanceof JSONArray) {
1314-
if (!((JSONArray)valueThis).similar(valueOther)) {
1315-
return false;
1316-
}
1317-
} else if (!valueThis.equals(valueOther)) {
1318-
return false;
1319-
}
1320-
}
1321-
return true;
1322-
} catch (Throwable exception) {
1323-
return false;
1324-
}
1325-
}
13261289

13271290
/**
13281291
* Try to convert a string into a number, boolean, or null. If the string

0 commit comments

Comments
 (0)