summaryrefslogtreecommitdiff
path: root/json
diff options
context:
space:
mode:
authorTobias Thierer <tobiast@google.com>2016-04-22 18:43:13 +0100
committerTobias Thierer <tobiast@google.com>2016-04-25 22:58:01 +0100
commitf0d4a04f3c7ac8dcb4a455084ca69721fbe9845e (patch)
treee7e6840d97227ff21113a27c267492bc4acb36bc /json
parent66e10c8590aa9107d09d2f16f64718e2261145a4 (diff)
Make broken JSONOBject.NULL.equals(null) consistent with Objects.hashCode(null)
Bug: 28189207 Change-Id: I76b8cc270dbf1b63f43c0dcd6970a703ad040da4
Diffstat (limited to 'json')
-rw-r--r--json/src/main/java/org/json/JSONObject.java3
-rw-r--r--json/src/test/java/org/json/JSONObjectTest.java7
2 files changed, 10 insertions, 0 deletions
diff --git a/json/src/main/java/org/json/JSONObject.java b/json/src/main/java/org/json/JSONObject.java
index 9ea91a6a11..7902389794 100644
--- a/json/src/main/java/org/json/JSONObject.java
+++ b/json/src/main/java/org/json/JSONObject.java
@@ -21,6 +21,7 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
// Note: this class was written without inspecting the non-free org.json sourcecode.
@@ -100,6 +101,8 @@ public class JSONObject {
@Override public boolean equals(Object o) {
return o == this || o == null; // API specifies this broken equals implementation
}
+ // at least make the broken equals(null) consistent with Objects.hashCode(null).
+ @Override public int hashCode() { return Objects.hashCode(null); }
@Override public String toString() {
return "null";
}
diff --git a/json/src/test/java/org/json/JSONObjectTest.java b/json/src/test/java/org/json/JSONObjectTest.java
index 9029ec6c6c..07d1cf6439 100644
--- a/json/src/test/java/org/json/JSONObjectTest.java
+++ b/json/src/test/java/org/json/JSONObjectTest.java
@@ -27,6 +27,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
+import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import junit.framework.TestCase;
@@ -825,6 +826,12 @@ public class JSONObjectTest extends TestCase {
assertTrue(object.isNull("bar"));
}
+ public void testNullValue_equalsAndHashCode() {
+ assertTrue(JSONObject.NULL.equals(null)); // guaranteed by javadoc
+ // not guaranteed by javadoc, but seems like a good idea
+ assertEquals(Objects.hashCode(null), JSONObject.NULL.hashCode());
+ }
+
public void testHas() throws JSONException {
JSONObject object = new JSONObject();
object.put("foo", 5);