Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 11, 2025
2 parents cc57658 + cfef132 commit 7c2c6a0
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 17 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Project: jackson-databind
(reported by Floris W)
#4953: Allow clearing all caches to avoid classloader leaks
(contributed by Joren I)
#4955: Add more remove methods for `ArrayNode`, `ObjectNode` [STEP-3]
#4959: Add explicit deserializer for `ThreadGroup`
#4961: Serialization for `JsonFormat.Shape.ARRAY` does not work when
there is `@JsonAnyGetter`
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tools/jackson/databind/node/ArrayNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;

import tools.jackson.core.*;
Expand Down Expand Up @@ -526,6 +527,12 @@ public ArrayNode removeAll()
return this;
}

@Override
public ArrayNode removeIf(Predicate<? super JsonNode> predicate) {
_children.removeIf(predicate);
return this;
}

/*
/**********************************************************************
/* Extended ArrayNode API, mutators, generic; addXxx()/insertXxx()/setXxx()
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/tools/jackson/databind/node/ContainerNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.function.Predicate;
import java.util.stream.Stream;

import tools.jackson.core.*;
Expand Down Expand Up @@ -175,4 +176,32 @@ public final NumericNode numberNode(long v) {
* @return Container node itself (to allow method call chaining)
*/
public abstract T removeAll();

/**
* Method for removing matching those children (value) nodes container has that
* match given predicate.
*
* @param predicate Predicate to use for matching: anything matching will be removed
*
* @return Container node itself (to allow method call chaining)
*
* @since 2.19
*/
public abstract T removeIf(Predicate<? super JsonNode> predicate);

/**
* Method for removing {@code null} children (value) nodes container has (that is,
* children for which {@code isNull()} returns true).
* Short-cut for:
*<pre>
* removeIf(JsonNode::isNull);
*<pre>
*
* @return Container node itself (to allow method call chaining)
*
* @since 2.19
*/
public T removeNulls() {
return removeIf(JsonNode::isNull);
}
}
7 changes: 7 additions & 0 deletions src/main/java/tools/jackson/databind/node/ObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.math.BigInteger;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

import tools.jackson.core.*;
Expand Down Expand Up @@ -711,6 +712,12 @@ public ObjectNode removeAll()
return this;
}

@Override
public ObjectNode removeIf(Predicate<? super JsonNode> predicate) {
_children.values().removeIf(predicate);
return this;
}

/**
* Method for removing all field properties out of this ObjectNode
* <b>except</b> for ones specified in argument.
Expand Down
60 changes: 46 additions & 14 deletions src/test/java/tools/jackson/databind/node/ArrayNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
public class ArrayNodeTest
extends DatabindTestUtil
{
private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testDirectCreation() throws Exception
{
Expand Down Expand Up @@ -112,7 +114,7 @@ public void testDirectCreation() throws Exception
@Test
public void testDirectCreation2() throws Exception
{
JsonNodeFactory f = objectMapper().getNodeFactory();
JsonNodeFactory f = MAPPER.getNodeFactory();
ArrayList<JsonNode> list = new ArrayList<>();
list.add(f.booleanNode(true));
list.add(f.textNode("foo"));
Expand Down Expand Up @@ -202,7 +204,7 @@ public void testArrayViaMapper() throws Exception
{
final String JSON = "[[[-0.027512,51.503221],[-0.008497,51.503221],[-0.008497,51.509744],[-0.027512,51.509744]]]";

JsonNode n = objectMapper().readTree(JSON);
JsonNode n = MAPPER.readTree(JSON);
assertNotNull(n);
assertTrue(n.isArray());
ArrayNode an = (ArrayNode) n;
Expand Down Expand Up @@ -241,7 +243,7 @@ public void testAdds()
@Test
public void testNullAdds()
{
JsonNodeFactory f = objectMapper().getNodeFactory();
JsonNodeFactory f = MAPPER.getNodeFactory();
ArrayNode array = f.arrayNode(14);

array.add((BigDecimal) null);
Expand Down Expand Up @@ -283,7 +285,7 @@ public void testAddAllWithNullInCollection()
@Test
public void testNullInserts()
{
JsonNodeFactory f = objectMapper().getNodeFactory();
JsonNodeFactory f = MAPPER.getNodeFactory();
ArrayNode array = f.arrayNode(3);

array.insert(0, (BigDecimal) null);
Expand All @@ -309,7 +311,7 @@ public void testNullInserts()
@Test
public void testNullSet()
{
JsonNodeFactory f = objectMapper().getNodeFactory();
JsonNodeFactory f = MAPPER.getNodeFactory();
ArrayNode array = f.arrayNode(3);

for (int i = 0; i < 14; i++) {
Expand Down Expand Up @@ -360,9 +362,8 @@ public void testNullChecking()
@Test
public void testNullChecking2()
{
ObjectMapper mapper = new ObjectMapper();
ArrayNode src = mapper.createArrayNode();
ArrayNode dest = mapper.createArrayNode();
ArrayNode src = MAPPER.createArrayNode();
ArrayNode dest = MAPPER.createArrayNode();
src.add("element");
dest.addAll(src);
}
Expand Down Expand Up @@ -420,7 +421,7 @@ public void testArrayNodeEquality()
@Test
public void testSimpleArray() throws Exception
{
ArrayNode result = objectMapper().createArrayNode();
ArrayNode result = MAPPER.createArrayNode();

assertTrue(result.isArray());
assertType(result, ArrayNode.class);
Expand Down Expand Up @@ -456,7 +457,7 @@ public void testSimpleArray() throws Exception
assertTrue(result.path(-100).isMissingNode());

// then construct and compare
ArrayNode array2 = objectMapper().createArrayNode();
ArrayNode array2 = MAPPER.createArrayNode();
array2.addNull();
array2.add(false);
assertEquals(result, array2);
Expand All @@ -476,9 +477,8 @@ public void testSimpleArray() throws Exception
@Test
public void testSimpleMismatch() throws Exception
{
ObjectMapper mapper = objectMapper();
try {
mapper.readValue(" 123 ", ArrayNode.class);
MAPPER.readValue(" 123 ", ArrayNode.class);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "from Integer value (token `JsonToken.VALUE_NUMBER_INT`)");
Expand All @@ -489,8 +489,7 @@ public void testSimpleMismatch() throws Exception
@Test
public void testStreamMethods()
{
ObjectMapper mapper = objectMapper();
ArrayNode arr = mapper.createArrayNode();
ArrayNode arr = MAPPER.createArrayNode();
arr.add(1).add("foo");
JsonNode n1 = arr.get(0);
JsonNode n2 = arr.get(1);
Expand All @@ -508,4 +507,37 @@ public void testStreamMethods()
// And then empty forEachEntry()
arr.forEachEntry((k, v) -> { throw new UnsupportedOperationException(); });
}

@Test
public void testRemoveAll() throws Exception
{
assertEquals(_arrayNode("[]"),
_arrayNode("['a', 2, null, true]").removeAll());
}

// [databind#4955]: remove methods
@Test
public void testRemoveIf() throws Exception
{
assertEquals(_arrayNode("[3]"),
_arrayNode("[2, 1, 3]")
.removeIf(value -> value.asInt() <= 2));
assertEquals(_arrayNode("[1]"),
_arrayNode("[1, 2, 3]")
.removeIf(value -> value.asInt() > 1));
}

// [databind#4955]: remove methods
@Test
public void testRemoveNulls() throws Exception
{
assertEquals(_arrayNode("[2]"),
_arrayNode("[null, null, 2, null]")
.removeNulls());
}

private ArrayNode _arrayNode(String json) throws Exception {
return (ArrayNode) MAPPER.readTree(a2q(json));
}

}
35 changes: 32 additions & 3 deletions src/test/java/tools/jackson/databind/node/ObjectNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,6 @@ public void testNullChecking()
assertEquals(4, o1.size());
}

/**
* Another test to verify [JACKSON-227]...
*/
@Test
public void testNullChecking2()
{
Expand Down Expand Up @@ -566,6 +563,38 @@ public void testStreamMethods()
assertEquals(obj.properties(), map.entrySet());
}

@Test
public void testRemoveAll() throws Exception
{
assertEquals(_objNode("{}"),
_objNode("{'a':1, 'b':2, 'c':3}").removeAll());
}

// [databind#4955]: remove methods
@Test
public void testRemoveIf() throws Exception
{
assertEquals(_objNode("{'c':3}"),
_objNode("{'a':1, 'b':2, 'c':3}")
.removeIf(value -> value.asInt() <= 2));
assertEquals(_objNode("{'a':1}"),
_objNode("{'a':1, 'b':2, 'c':3}")
.removeIf(value -> value.asInt() > 1));
}

// [databind#4955]: remove methods
@Test
public void testRemoveNulls() throws Exception
{
assertEquals(_objNode("{'b':2}"),
_objNode("{'a':null,'b':2,'c':null}")
.removeNulls());
}

private ObjectNode _objNode(String json) throws Exception {
return (ObjectNode) MAPPER.readTree(a2q(json));
}

private String _toString(JsonNode n) {
return n.properties().stream()
.map(e -> e.getKey() + "/" + e.getValue())
Expand Down

0 comments on commit 7c2c6a0

Please sign in to comment.