Skip to content

Commit cfbc306

Browse files
committed
Fixes Issue stleary#611 JsonObject.similar() returns after number entry check
1 parent 449ec87 commit cfbc306

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,9 @@ public boolean similar(Object other) {
20922092
return false;
20932093
}
20942094
} else if (valueThis instanceof Number && valueOther instanceof Number) {
2095-
return isNumberSimilar((Number)valueThis, (Number)valueOther);
2095+
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
2096+
return false;
2097+
};
20962098
} else if (!valueThis.equals(valueOther)) {
20972099
return false;
20982100
}

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public class JSONObjectTest {
100100
@Test
101101
public void verifySimilar() {
102102
final String string1 = "HasSameRef";
103+
final String string2 = "HasDifferentRef";
103104
JSONObject obj1 = new JSONObject()
104105
.put("key1", "abc")
105106
.put("key2", 2)
@@ -119,12 +120,16 @@ public void verifySimilar() {
119120
.put("key1", "abc")
120121
.put("key2", 2.0)
121122
.put("key3", new String(string1));
122-
123-
assertFalse("Should eval to false", obj1.similar(obj2));
124123

125-
assertTrue("Should eval to true", obj1.similar(obj3));
124+
JSONObject obj5 = new JSONObject()
125+
.put("key1", "abc")
126+
.put("key2", 2.0)
127+
.put("key3", new String(string2));
126128

127-
assertTrue("Should eval to true", obj1.similar(obj4));
129+
assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2));
130+
assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3));
131+
assertTrue("obj1-obj4 Should eval to true", obj1.similar(obj4));
132+
assertFalse("obj1-obj5 Should eval to false", obj1.similar(obj5));
128133

129134
}
130135

0 commit comments

Comments
 (0)