Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

equals method #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -916,4 +916,29 @@ public Writer write(Writer writer) throws JSONException {
throw new JSONException(e);
}
}
}

/**
* * check if this JSONArray is equal to another.
* * @param o The object to check for equality
* * @return true if o is a JSONArray and all corresponding values stored in there are equal, otherwise false
* */
@Override
public boolean equals(Object o) {
if(o == null || !(o instanceof JSONArray)) {
return false;
}

JSONArray other = (JSONArray) o;
for(int i = 0; i < other.length(); i++) {
try {
if(!this.get(i).equals(other.get(i))) {
return false;
}
} catch (JSONException e) {
return false;
}
}

return true;
}
}
33 changes: 32 additions & 1 deletion JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1633,4 +1633,35 @@ public Writer write(Writer writer) throws JSONException {
throw new JSONException(exception);
}
}
}

/**
* Check if two JSONObjects are equal
* @param o The object to check for equality
* @return true if o is a JSONObject and all fields are equal to those
* of this object, otherwise false
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof JSONObject)) {
return false;
}

JSONObject other = (JSONObject) o;

Iterator keys = this.keys();
while (keys.hasNext()) {
try {
Object key = keys.next();
Object mine = this.map.get(key);
if(!other.get(key.toString()).equals(mine)) {
return false;
}
} catch (JSONException ex) {
return false;
}

}

return true;
}
}
24 changes: 23 additions & 1 deletion Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,28 @@ public void testJSON() throws Exception {
ja = JSONML.toJSONArray(string);
assertEquals("[\"Root\",[\"MsgType\",{\"type\":\"node\"},[\"BatchType\",{\"type\":\"string\"},111111111111111]]]",
ja.toString());

// equals method
jsonobject = new JSONObject();
jsonobject.put("val1", "blabla");
jsonobject.put("val2", "fooo");

JSONObject jso2 = new JSONObject();
jso2.put("val2", "fooo");
jso2.put("val1", "blabla");

JSONObject jso3 = new JSONObject();
jso3.put("val1", "nope");
jso3.put("val2", "fooo");

JSONObject jso4 = new JSONObject();
jso4.put("val2", "fooo");

assertTrue(jsonobject.equals(jso2));
assertFalse(jsonobject.equals(jso3));
assertFalse(jsonobject.equals(jso4));
assertFalse(jsonobject.equals(jsonarray));
assertFalse(jsonarray.equals(null));
}

public void testExceptions() throws Exception {
Expand Down Expand Up @@ -928,4 +950,4 @@ public String toString() {
this.isBoolean() + "." + this.getBENT() + " " + this.getX();
}
}
}
}