Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[json-node] Add JsonObject.remove() method #312

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ public JsonObject add(String key, long value) {
return add(key, JsonLong.of(value));
}

/**
* Remove the node for the given key returning the removed value.
*
* @param key The key to remove.
* @return The node that has been removed.
*/
@Nullable
public JsonNode remove(String key) {
return children.remove(key);
}

/**
* Return the direct element at the given path throwing IllegalArgumentException
* if it is missing.
Expand Down
14 changes: 14 additions & 0 deletions json-node/src/test/java/io/avaje/json/node/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ void add() {
assertThat(obj.size()).isEqualTo(1);
}

@Test
void remove() {
var obj = JsonObject.create().add("one", 1).add("two", 2).add("three", 3);
assertThat(obj.elements().keySet()).containsExactly("one", "two", "three");

JsonNode two = obj.remove("two");
assertThat(two).isNotNull();
assertThat(two).isInstanceOf(JsonInteger.class);
assertThat(two.text()).isEqualTo("2");

assertThat(obj.elements().keySet()).containsExactly("one", "three");
assertThat(obj.toString()).isEqualTo("{one=1, three=3}");
}

@Test
void get() {
JsonNode name = basicObject.get("name");
Expand Down
Loading