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

JsonObject.similar() number entry check bug fix #613

Merged
merged 4 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,9 @@ public boolean similar(Object other) {
return false;
}
} else if (valueThis instanceof Number && valueOther instanceof Number) {
return isNumberSimilar((Number)valueThis, (Number)valueOther);
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
return false;
};
} else if (!valueThis.equals(valueOther)) {
return false;
}
Expand Down
13 changes: 9 additions & 4 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class JSONObjectTest {
@Test
public void verifySimilar() {
final String string1 = "HasSameRef";
final String string2 = "HasDifferentRef";
JSONObject obj1 = new JSONObject()
.put("key1", "abc")
.put("key2", 2)
Expand All @@ -119,12 +120,16 @@ public void verifySimilar() {
.put("key1", "abc")
.put("key2", 2.0)
.put("key3", new String(string1));

assertFalse("Should eval to false", obj1.similar(obj2));

assertTrue("Should eval to true", obj1.similar(obj3));
JSONObject obj5 = new JSONObject()
.put("key1", "abc")
.put("key2", 2.0)
.put("key3", new String(string2));

assertTrue("Should eval to true", obj1.similar(obj4));
assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2));
assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3));
assertTrue("obj1-obj4 Should eval to true", obj1.similar(obj4));
assertFalse("obj1-obj5 Should eval to false", obj1.similar(obj5));

}

Expand Down