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
|
@ -75,7 +75,7 @@ import java.util.Map;
|
|||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2014-04-18
|
||||
* @version 2014-04-21
|
||||
*/
|
||||
public class JSONArray {
|
||||
|
||||
|
@ -813,11 +813,42 @@ public class JSONArray {
|
|||
* was no value.
|
||||
*/
|
||||
public Object remove(int index) {
|
||||
Object o = this.opt(index);
|
||||
if (index >= 0 && index < this.length()) {
|
||||
this.myArrayList.remove(index);
|
||||
return index >= 0 && index < this.length()
|
||||
? this.myArrayList.remove(index)
|
||||
: null;
|
||||
}
|
||||
return o;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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