mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
similar
This commit is contained in:
parent
cdaaf12557
commit
7ff3fa4e40
2 changed files with 77 additions and 6 deletions
|
@ -90,7 +90,7 @@ import java.util.Set;
|
|||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2013-06-17
|
||||
* @version 2014-04-21
|
||||
*/
|
||||
public class JSONObject {
|
||||
/**
|
||||
|
@ -1281,6 +1281,46 @@ public class JSONObject {
|
|||
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
|
||||
* can't be converted, return the string.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue