@@ -90,7 +90,7 @@ of this software and associated documentation files (the "Software"), to deal
9090 * </ul>
9191 *
9292 * @author JSON.org
93- * @version 2013-06-17
93+ * @version 2014-04-21
9494 */
9595public class JSONObject {
9696 /**
@@ -1281,6 +1281,46 @@ public Object remove(String key) {
12811281 return this .map .remove (key );
12821282 }
12831283
1284+ /**
1285+ * Determine if two JSONObjects are similar.
1286+ * They must contain the same set of names which must be associated with
1287+ * similar values.
1288+ *
1289+ * @param other The other JSONObject
1290+ * @return true if they are equal
1291+ */
1292+ public boolean similar (Object other ) {
1293+ try {
1294+ if (!(other instanceof JSONObject )) {
1295+ return false ;
1296+ }
1297+ Set set = this .keySet ();
1298+ if (!set .equals (((JSONObject )other ).keySet ())) {
1299+ return false ;
1300+ }
1301+ Iterator iterator = set .iterator ();
1302+ while (iterator .hasNext ()) {
1303+ String name = (String )iterator .next ();
1304+ Object valueThis = this .get (name );
1305+ Object valueOther = ((JSONObject )other ).get (name );
1306+ if (valueThis instanceof JSONObject ) {
1307+ if (!((JSONObject )valueThis ).similar (valueOther )) {
1308+ return false ;
1309+ }
1310+ } else if (valueThis instanceof JSONArray ) {
1311+ if (!((JSONArray )valueThis ).similar (valueOther )) {
1312+ return false ;
1313+ }
1314+ } else if (!valueThis .equals (valueOther )) {
1315+ return false ;
1316+ }
1317+ }
1318+ return true ;
1319+ } catch (Throwable exception ) {
1320+ return false ;
1321+ }
1322+ }
1323+
12841324 /**
12851325 * Try to convert a string into a number, boolean, or null. If the string
12861326 * can't be converted, return the string.
0 commit comments