-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Why we Overriding Equals in JSONObject and JSONArray? the super classes have it implemented correctly. both your past and current implementations I don't quite understand the use of this is the corrected version of JSONObject#equals . Note: not optimized it expects null first instead of nonull. Im too lazy to fix it. I am wondering why we overriding it to begin with is there a specific use where you wanted null or non null combos to suddenly make the equals return false? Since I expect the equals to behave like normal java I am going to remove them in my fork of this.
@Override
public boolean equals(Object object) {
if(object != null && object instanceof Map) {
Map<?, ?> map = (Map<?, ?>)object;
if(this.size() == map.size()) {
for(Map.Entry<Object, Object> thisEntry : this.entrySet()) {
Object key = thisEntry.getKey();
Object value = thisEntry.getValue();
if(!map.containsKey(key) || !( value == null ? (map.get(key) == null) : (value.equals(map.get(key))) ) ) {
return false;
}
}
return true;
}
}
return false;
}
I could maybe understand the JSONArray#equals as the super classes default check doesn't check for size before comparing and is unoptimized but this can be done with so little code. NOTE: all modern java probably since even java 6 will return false for instanceof on null objects So we don't need the object != null check. Correct me if I am wrong though because seen alot of older java code that had != null and then instanceof checks.
@Override
public boolean equals(Object object)
{
return object instanceof JSONArray && this.size() == ((JSONArray)object).size() && super.equals(object);
}
And finally the final JSONObject#equals
@Override
public boolean equals(Object o)
{
return o instanceof JSONObject && this.size() == ((JSONObject)o).size() && super.equals(o);
}