Skip to content

Commit 7ff3fa4

Browse files
similar
1 parent cdaaf12 commit 7ff3fa4

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

JSONArray.java

+36-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ of this software and associated documentation files (the "Software"), to deal
7575
* </ul>
7676
*
7777
* @author JSON.org
78-
* @version 2014-04-18
78+
* @version 2014-04-21
7979
*/
8080
public class JSONArray {
8181

@@ -813,11 +813,42 @@ public JSONArray put(int index, Object value) throws JSONException {
813813
* was no value.
814814
*/
815815
public Object remove(int index) {
816-
Object o = this.opt(index);
817-
if (index >= 0 && index < this.length()) {
818-
this.myArrayList.remove(index);
816+
return index >= 0 && index < this.length()
817+
? this.myArrayList.remove(index)
818+
: null;
819+
}
820+
821+
/**
822+
* Determine if two JSONArrays are similar.
823+
* They must contain similar sequences.
824+
*
825+
* @param other The other JSONArray
826+
* @return true if they are equal
827+
*/
828+
public boolean similar(Object other) {
829+
if (!(other instanceof JSONArray)) {
830+
return false;
831+
}
832+
int len = this.length();
833+
if (len != ((JSONArray)other).length()) {
834+
return false;
835+
}
836+
for (int i = 0; i < len; i += 1) {
837+
Object valueThis = this.get(i);
838+
Object valueOther = ((JSONArray)other).get(i);
839+
if (valueThis instanceof JSONObject) {
840+
if (!((JSONObject)valueThis).similar(valueOther)) {
841+
return false;
842+
}
843+
} else if (valueThis instanceof JSONArray) {
844+
if (!((JSONArray)valueThis).similar(valueOther)) {
845+
return false;
846+
}
847+
} else if (!valueThis.equals(valueOther)) {
848+
return false;
849+
}
819850
}
820-
return o;
851+
return true;
821852
}
822853

823854
/**

JSONObject.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -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
*/
9595
public 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

Comments
 (0)