Skip to content

Commit 97b33cb

Browse files
committed
Fix JsonArray contains for non-wrapped JsonArray/JsonObject
1 parent 14e9377 commit 97b33cb

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

vertx-core/src/main/java/io/vertx/core/json/JsonArray.java

+37-4
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,47 @@ public JsonArray set(int pos, Object value) {
449449
}
450450

451451
/**
452-
* Does the JSON array contain the specified value? This method will scan the entire array until it finds a value
453-
* or reaches the end.
452+
* Returns {@code true} if this JSON Array contains the specified value.
453+
* More formally, returns {@code true} if and only if this JSON array contains
454+
* at least one entry {@code entry} such that {@code Objects.equals(value, entry)}.
454455
*
455-
* @param value the value
456+
* @param value the value whose presence in this JSON array is to be tested
456457
* @return true if it contains the value, false if not
457458
*/
458459
public boolean contains(Object value) {
459-
return list.contains(value);
460+
return indexOf(value) >= 0;
461+
}
462+
463+
/**
464+
* Returns the index of the last occurrence of the specified value
465+
* in this JSON array, or -1 if this JSON array does not contain the value.
466+
* More formally, returns the highest index {@code i} such that
467+
* {@code Objects.equals(value, get(i))},
468+
* or -1 if there is no such index.
469+
*
470+
* @param value the value whose index in this JSON array is to be returned
471+
* @return the index of the value in the array, or -1 if the value is not in the array
472+
*/
473+
public int indexOf(Object value) {
474+
// in case of JsonObject/JsonArray, the list might still contain an unwrapped Map/List, we need to check for both
475+
if (value instanceof JsonObject) {
476+
return indexOfFirst(value, ((JsonObject) value).getMap());
477+
} else if (value instanceof JsonArray) {
478+
return indexOfFirst(value, ((JsonArray) value).getList());
479+
} else {
480+
return list.indexOf(value);
481+
}
482+
}
483+
484+
private int indexOfFirst(Object value, Object value2) {
485+
for (int i = 0; i < list.size(); i++) {
486+
Object entry = list.get(i);
487+
if (value.equals(entry) || value2.equals(entry)) {
488+
return i;
489+
}
490+
}
491+
492+
return -1;
460493
}
461494

462495
/**

vertx-core/src/test/java/io/vertx/tests/json/JsonArrayTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ public void testContains() {
658658
JsonArray arr = new JsonArray();
659659
jsonArray.add(obj);
660660
jsonArray.add(arr);
661+
jsonArray.add(Map.of("foo", "bar"));
662+
jsonArray.add(List.of("baz"));
661663
assertFalse(jsonArray.contains("eek"));
662664
assertFalse(jsonArray.contains(false));
663665
assertFalse(jsonArray.contains(321));
@@ -668,6 +670,8 @@ public void testContains() {
668670
assertTrue(jsonArray.contains(123));
669671
assertTrue(jsonArray.contains(obj));
670672
assertTrue(jsonArray.contains(arr));
673+
assertTrue(jsonArray.contains(new JsonObject().put("foo", "bar")));
674+
assertTrue(jsonArray.contains(new JsonArray().add("baz")));
671675
}
672676

673677
@Test

0 commit comments

Comments
 (0)