From 7ff3fa4e40cfd5b44570297080810aed5cce6086 Mon Sep 17 00:00:00 2001 From: Douglas Crockford Date: Mon, 21 Apr 2014 16:11:51 -0700 Subject: [PATCH] similar --- JSONArray.java | 41 ++++++++++++++++++++++++++++++++++++----- JSONObject.java | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/JSONArray.java b/JSONArray.java index e0af8fa..e864a1e 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -75,7 +75,7 @@ import java.util.Map; * * * @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; + } + + /** + * 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; } /** diff --git a/JSONObject.java b/JSONObject.java index 5ca5a45..20ba42f 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -90,7 +90,7 @@ import java.util.Set; * * * @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.