JsonObject equality should check the map keys instead of the sizes -closes #3170

This commit is contained in:
Julien Viet
2019-11-03 09:11:20 +01:00
parent 7b8734b46c
commit b6b718470e
3 changed files with 23 additions and 18 deletions

View File

@@ -806,16 +806,13 @@ public class JsonArray implements Iterable<Object>, ClusterSerializable, Shareab
} else {
return false;
}
if (l1.size() != l2.size())
if (l1.size() != l2.size()) {
return false;
}
Iterator<?> iter = l2.iterator();
for (Object entry : l1) {
Object other = iter.next();
if (entry == null) {
if (other != null) {
return false;
}
} else if (!JsonObject.equals(entry, other)) {
if (entry == null ? other != null : !JsonObject.equals(entry, other)) {
return false;
}
}

View File

@@ -895,7 +895,7 @@ public class JsonObject implements Iterable<Map.Entry<String, Object>>, ClusterS
return objectEquals(map, o);
}
static boolean objectEquals(Map<?, ?> m1, Object o2) {
private static boolean objectEquals(Map<?, ?> m1, Object o2) {
Map<?, ?> m2;
if (o2 instanceof JsonObject) {
m2 = ((JsonObject) o2).map;
@@ -904,26 +904,23 @@ public class JsonObject implements Iterable<Map.Entry<String, Object>>, ClusterS
} else {
return false;
}
if (m1.size() != m2.size())
if (!m1.keySet().equals(m2.keySet())) {
return false;
}
for (Map.Entry<?, ?> entry : m1.entrySet()) {
Object val = entry.getValue();
if (val == null) {
if (m2.get(entry.getKey()) != null) {
return false;
}
} else {
if (!equals(entry.getValue(), m2.get(entry.getKey()))) {
return false;
}
Object val1 = entry.getValue();
Object val2 = m2.get(entry.getKey());
if (val1 == null ? val2 != null : !equals(val1, val2)) {
return false;
}
}
return true;
}
static boolean equals(Object o1, Object o2) {
if (o1 == o2)
if (o1 == o2) {
return true;
}
if (o1 instanceof JsonObject) {
return objectEquals(((JsonObject) o1).map, o2);
}

View File

@@ -1703,6 +1703,17 @@ public class JsonObjectTest {
} catch (NullPointerException ignore) {
}
}
@Test
public void testEquals() {
assertNotEquals(new JsonObject().put("a", 1), new JsonObject().put("b", 1));
assertNotEquals(new JsonObject().put("a", 1), new JsonObject());
assertNotEquals(new JsonObject(), new JsonObject().put("b", 1));
assertNotEquals(new JsonObject().put("a", 1), new JsonObject().put("a", 2));
assertNotEquals(new JsonObject().put("a", 1), new JsonObject().putNull("a"));
assertNotEquals(new JsonObject().putNull("a"), new JsonObject().put("a", 1));
assertEquals(new JsonObject().putNull("a"), new JsonObject().putNull("a"));
}
}