Skip to content

Commit c6089e5

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

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/main/java/org/json/JSONArray.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,9 @@ public boolean similar(Object other) {
13831383
return false;
13841384
}
13851385
} else if (valueThis instanceof Number && valueOther instanceof Number) {
1386-
return JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther);
1386+
if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) {
1387+
return false;
1388+
}
13871389
} else if (!valueThis.equals(valueOther)) {
13881390
return false;
13891391
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public class JSONArrayTest {
8787
@Test
8888
public void verifySimilar() {
8989
final String string1 = "HasSameRef";
90+
final String string2 = "HasDifferentRef";
9091
JSONArray obj1 = new JSONArray()
9192
.put("abc")
9293
.put(string1)
@@ -101,10 +102,20 @@ public void verifySimilar() {
101102
.put("abc")
102103
.put(new String(string1))
103104
.put(2);
105+
106+
JSONArray obj4 = new JSONArray()
107+
.put("abc")
108+
.put(2.0)
109+
.put(new String(string1));
110+
111+
JSONArray obj5 = new JSONArray()
112+
.put("abc")
113+
.put(2.0)
114+
.put(new String(string2));
104115

105-
assertFalse("Should eval to false", obj1.similar(obj2));
106-
107-
assertTrue("Should eval to true", obj1.similar(obj3));
116+
assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2));
117+
assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3));
118+
assertFalse("obj4-obj5 Should eval to false", obj4.similar(obj5));
108119
}
109120

110121
/**

0 commit comments

Comments
 (0)