mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Merge pull request #613 from stleary/fix-similar-check
Fixes Issue #611 JsonObject.similar() returns after number entry check
This commit is contained in:
commit
c0e467c848
@ -1383,7 +1383,9 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (valueThis instanceof Number && valueOther instanceof Number) {
|
} 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)) {
|
} else if (!valueThis.equals(valueOther)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -2108,7 +2108,9 @@ public class JSONObject {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (valueThis instanceof Number && valueOther instanceof Number) {
|
} 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)) {
|
} else if (!valueThis.equals(valueOther)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -87,6 +87,7 @@ public class JSONArrayTest {
|
|||||||
@Test
|
@Test
|
||||||
public void verifySimilar() {
|
public void verifySimilar() {
|
||||||
final String string1 = "HasSameRef";
|
final String string1 = "HasSameRef";
|
||||||
|
final String string2 = "HasDifferentRef";
|
||||||
JSONArray obj1 = new JSONArray()
|
JSONArray obj1 = new JSONArray()
|
||||||
.put("abc")
|
.put("abc")
|
||||||
.put(string1)
|
.put(string1)
|
||||||
@ -101,10 +102,20 @@ public class JSONArrayTest {
|
|||||||
.put("abc")
|
.put("abc")
|
||||||
.put(new String(string1))
|
.put(new String(string1))
|
||||||
.put(2);
|
.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));
|
assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2));
|
||||||
|
assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3));
|
||||||
assertTrue("Should eval to true", obj1.similar(obj3));
|
assertFalse("obj4-obj5 Should eval to false", obj4.similar(obj5));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,6 +100,7 @@ public class JSONObjectTest {
|
|||||||
@Test
|
@Test
|
||||||
public void verifySimilar() {
|
public void verifySimilar() {
|
||||||
final String string1 = "HasSameRef";
|
final String string1 = "HasSameRef";
|
||||||
|
final String string2 = "HasDifferentRef";
|
||||||
JSONObject obj1 = new JSONObject()
|
JSONObject obj1 = new JSONObject()
|
||||||
.put("key1", "abc")
|
.put("key1", "abc")
|
||||||
.put("key2", 2)
|
.put("key2", 2)
|
||||||
@ -119,16 +120,23 @@ public class JSONObjectTest {
|
|||||||
.put("key1", "abc")
|
.put("key1", "abc")
|
||||||
.put("key2", 2.0)
|
.put("key2", 2.0)
|
||||||
.put("key3", new String(string1));
|
.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")
|
||||||
assertTrue("Should eval to true", obj1.similar(obj4));
|
.put("key2", 2.0)
|
||||||
|
.put("key3", new String(string2));
|
||||||
|
|
||||||
|
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));
|
||||||
// verify that a double and big decimal are "similar"
|
// 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}")));
|
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
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user