1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00
This commit is contained in:
Douglas Crockford 2014-04-21 16:11:51 -07:00
parent cdaaf12557
commit 7ff3fa4e40
2 changed files with 77 additions and 6 deletions

View file

@ -75,7 +75,7 @@ import java.util.Map;
* </ul> * </ul>
* *
* @author JSON.org * @author JSON.org
* @version 2014-04-18 * @version 2014-04-21
*/ */
public class JSONArray { public class JSONArray {
@ -813,11 +813,42 @@ public class JSONArray {
* was no value. * was no value.
*/ */
public Object remove(int index) { public Object remove(int index) {
Object o = this.opt(index); return index >= 0 && index < this.length()
if (index >= 0 && index < this.length()) { ? this.myArrayList.remove(index)
this.myArrayList.remove(index); : null;
}
/**
* Determine if two JSONArrays are similar.
* They must contain similar sequences.
*
* @param other The other JSONArray
* @return true if they are equal
*/
public boolean similar(Object other) {
if (!(other instanceof JSONArray)) {
return false;
} }
return o; int len = this.length();
if (len != ((JSONArray)other).length()) {
return false;
}
for (int i = 0; i < len; i += 1) {
Object valueThis = this.get(i);
Object valueOther = ((JSONArray)other).get(i);
if (valueThis instanceof JSONObject) {
if (!((JSONObject)valueThis).similar(valueOther)) {
return false;
}
} else if (valueThis instanceof JSONArray) {
if (!((JSONArray)valueThis).similar(valueOther)) {
return false;
}
} else if (!valueThis.equals(valueOther)) {
return false;
}
}
return true;
} }
/** /**

View file

@ -90,7 +90,7 @@ import java.util.Set;
* </ul> * </ul>
* *
* @author JSON.org * @author JSON.org
* @version 2013-06-17 * @version 2014-04-21
*/ */
public class JSONObject { public class JSONObject {
/** /**
@ -1281,6 +1281,46 @@ public class JSONObject {
return this.map.remove(key); return this.map.remove(key);
} }
/**
* Determine if two JSONObjects are similar.
* They must contain the same set of names which must be associated with
* similar values.
*
* @param other The other JSONObject
* @return true if they are equal
*/
public boolean similar(Object other) {
try {
if (!(other instanceof JSONObject)) {
return false;
}
Set set = this.keySet();
if (!set.equals(((JSONObject)other).keySet())) {
return false;
}
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
String name = (String)iterator.next();
Object valueThis = this.get(name);
Object valueOther = ((JSONObject)other).get(name);
if (valueThis instanceof JSONObject) {
if (!((JSONObject)valueThis).similar(valueOther)) {
return false;
}
} else if (valueThis instanceof JSONArray) {
if (!((JSONArray)valueThis).similar(valueOther)) {
return false;
}
} else if (!valueThis.equals(valueOther)) {
return false;
}
}
return true;
} catch (Throwable exception) {
return false;
}
}
/** /**
* Try to convert a string into a number, boolean, or null. If the string * Try to convert a string into a number, boolean, or null. If the string
* can't be converted, return the string. * can't be converted, return the string.