From cfbc306673ba3a4bec76ef6607db6401beb9c09b Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 18 Jul 2021 10:32:49 -0500 Subject: [PATCH 1/3] Fixes Issue #611 JsonObject.similar() returns after number entry check --- src/main/java/org/json/JSONObject.java | 4 +++- src/test/java/org/json/junit/JSONObjectTest.java | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 97a6df8d2..6d7d340c6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -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; } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 0d5acdd2e..39c020fe6 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -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) @@ -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)); } From c6089e53f553b1432e047a53bb13d6bdd999d26d Mon Sep 17 00:00:00 2001 From: stleary Date: Sun, 18 Jul 2021 19:53:23 -0500 Subject: [PATCH 2/3] Fixes Issue #611 JsonArray.similar() returns after number entry check --- src/main/java/org/json/JSONArray.java | 4 +++- src/test/java/org/json/junit/JSONArrayTest.java | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 342b91e99..1e6a8a6f9 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -1383,7 +1383,9 @@ public boolean similar(Object other) { return false; } } else if (valueThis instanceof Number && valueOther instanceof Number) { - return JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther); + if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) { + return false; + } } else if (!valueThis.equals(valueOther)) { return false; } diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index d0980ef61..eafda51d9 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -87,6 +87,7 @@ public class JSONArrayTest { @Test public void verifySimilar() { final String string1 = "HasSameRef"; + final String string2 = "HasDifferentRef"; JSONArray obj1 = new JSONArray() .put("abc") .put(string1) @@ -101,10 +102,20 @@ public void verifySimilar() { .put("abc") .put(new String(string1)) .put(2); + + JSONArray obj4 = new JSONArray() + .put("abc") + .put(2.0) + .put(new String(string1)); + + JSONArray obj5 = new JSONArray() + .put("abc") + .put(2.0) + .put(new String(string2)); - assertFalse("Should eval to false", obj1.similar(obj2)); - - assertTrue("Should eval to true", obj1.similar(obj3)); + assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2)); + assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3)); + assertFalse("obj4-obj5 Should eval to false", obj4.similar(obj5)); } /** From 8680b10716970e17a7e5448b4b8d83339ab01773 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 26 Jul 2021 18:07:38 -0500 Subject: [PATCH 3/3] merge from master to pick up #616 and add one more test --- src/test/java/org/json/junit/JSONObjectTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e98c419b2..9ddbc2ec2 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -130,10 +130,13 @@ public void verifySimilar() { 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)); - // verify that a double and big decimal are "similar" assertTrue("should eval to true",new JSONObject().put("a",1.1d).similar(new JSONObject("{\"a\":1.1}"))); - + // Confirm #618 is fixed (compare should not exit early if similar numbers are found) + // Note that this test may not work if the JSONObject map entry order changes + JSONObject first = new JSONObject("{\"a\": 1, \"b\": 2, \"c\": 3}"); + JSONObject second = new JSONObject("{\"a\": 1, \"b\": 2.0, \"c\": 4}"); + assertFalse("first-second should eval to false", first.similar(second)); } @Test