Skip to content

Commit c0e467c

Browse files
authored
Merge pull request #613 from stleary/fix-similar-check
Fixes Issue #611 JsonObject.similar() returns after number entry check
2 parents bb048e3 + 8680b10 commit c0e467c

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

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/main/java/org/json/JSONObject.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2108,7 +2108,9 @@ public boolean similar(Object other) {
21082108
return false;
21092109
}
21102110
} else if (valueThis instanceof Number && valueOther instanceof Number) {
2111-
return isNumberSimilar((Number)valueThis, (Number)valueOther);
2111+
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
2112+
return false;
2113+
};
21122114
} else if (!valueThis.equals(valueOther)) {
21132115
return false;
21142116
}

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
/**

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

Lines changed: 14 additions & 6 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,16 +120,23 @@ 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));
126-
127-
assertTrue("Should eval to true", obj1.similar(obj4));
124+
JSONObject obj5 = new JSONObject()
125+
.put("key1", "abc")
126+
.put("key2", 2.0)
127+
.put("key3", new String(string2));
128128

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));
129133
// verify that a double and big decimal are "similar"
130134
assertTrue("should eval to true",new JSONObject().put("a",1.1d).similar(new JSONObject("{\"a\":1.1}")));
131-
135+
// Confirm #618 is fixed (compare should not exit early if similar numbers are found)
136+
// Note that this test may not work if the JSONObject map entry order changes
137+
JSONObject first = new JSONObject("{\"a\": 1, \"b\": 2, \"c\": 3}");
138+
JSONObject second = new JSONObject("{\"a\": 1, \"b\": 2.0, \"c\": 4}");
139+
assertFalse("first-second should eval to false", first.similar(second));
132140
}
133141

134142
@Test

0 commit comments

Comments
 (0)