Skip to content

Commit

Permalink
Fix JsonArray contains for non-wrapped JsonArray/JsonObject
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed Dec 4, 2024
1 parent 2d361cf commit cf0ac28
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/main/java/io/vertx/core/json/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,47 @@ public JsonArray set(int pos, Object value) {
}

/**
* Does the JSON array contain the specified value? This method will scan the entire array until it finds a value
* or reaches the end.
* Returns {@code true} if this JSON Array contains the specified value.
* More formally, returns {@code true} if and only if this JSON array contains
* at least one entry {@code entry} such that {@code Objects.equals(value, entry)}.
*
* @param value the value
* @param value the value whose presence in this JSON array is to be tested
* @return true if it contains the value, false if not
*/
public boolean contains(Object value) {
return list.contains(value);
return indexOf(value) >= 0;
}

/**
* Returns the index of the last occurrence of the specified value
* in this JSON array, or -1 if this JSON array does not contain the value.
* More formally, returns the highest index {@code i} such that
* {@code Objects.equals(value, get(i))},
* or -1 if there is no such index.
*
* @param value the value whose index in this JSON array is to be returned
* @return the index of the value in the array, or -1 if the value is not in the array
*/
public int indexOf(Object value) {
// in case of JsonObject/JsonArray, the list might still contain an unwrapped Map/List, we need to check for both
if (value instanceof JsonObject) {
return indexOfFirst(value, ((JsonObject) value).getMap());
} else if (value instanceof JsonArray) {
return indexOfFirst(value, ((JsonArray) value).getList());
} else {
return list.indexOf(value);
}
}

private int indexOfFirst(Object value, Object value2) {
for (int i = 0; i < list.size(); i++) {
Object entry = list.get(i);
if (value.equals(entry) || value2.equals(entry)) {
return i;
}
}

return -1;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/io/vertx/core/json/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ public void testContains() {
JsonArray arr = new JsonArray();
jsonArray.add(obj);
jsonArray.add(arr);
jsonArray.add(Collections.singletonMap("foo", "bar"));
jsonArray.add(Collections.singletonList("baz"));
assertFalse(jsonArray.contains("eek"));
assertFalse(jsonArray.contains(false));
assertFalse(jsonArray.contains(321));
Expand All @@ -668,6 +670,8 @@ public void testContains() {
assertTrue(jsonArray.contains(123));
assertTrue(jsonArray.contains(obj));
assertTrue(jsonArray.contains(arr));
assertTrue(jsonArray.contains(new JsonObject().put("foo", "bar")));
assertTrue(jsonArray.contains(new JsonArray().add("baz")));
}

@Test
Expand Down

0 comments on commit cf0ac28

Please sign in to comment.