Skip to content

Commit

Permalink
Merge branch 'master' into spliterator
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 30, 2024
2 parents 074b859 + 5c6ec83 commit 3986751
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Project: jackson-databind
(requested by @nathanukey)
#4849 Not able to deserialize Enum with default typing after upgrading 2.15.4 -> 2.17.1
(reported by Kornel Zemla)
#4863: Add basic Stream support in `JsonNode`: `valueStream()`, `entryStream()`,
#4863: Add basic Stream support in `JsonNode`: `valueStream()`, `propertyStream()`,
`forEachEntry()`

2.18.3 (not yet released)
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/tools/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1002,9 +1002,14 @@ public Spliterator<JsonNode> valuesSpliterator() {
}

/**
* NOTE: This method is deprecated, use {@link #properties()} instead.
*
* @return Iterator that can be used to traverse all key/value pairs for
* object nodes; empty iterator (no contents) for other types
*
* @deprecated As of 2.19, replaced by {@link #properties()}
*/
@Deprecated // since 2.19
public Iterator<Map.Entry<String, JsonNode>> fields() {
return ClassUtil.emptyIterator();
}
Expand All @@ -1022,6 +1027,7 @@ public Spliterator<Map.Entry<String, JsonNode>> fieldsSpliterator() {
* Accessor that will return properties of {@code ObjectNode}
* similar to how {@link Map#entrySet()} works;
* for other node types will return empty {@link java.util.Set}.
* Replacement for {@link JsonNode#fields()}.
*
* @return Set of properties, if this node is an {@code ObjectNode}
* ({@link JsonNode#isObject()} returns {@code true}); empty
Expand All @@ -1046,24 +1052,26 @@ public Stream<JsonNode> valueStream() {
}

/**
* Returns a stream of all value nodes of this Node, iff
* this node is an an {@code ObjectNode}.
* Returns a stream of all properties (key, value pairs) of this Node,
* iff this node is an an {@code ObjectNode}.
* For other types of nodes, returns empty stream.
*
* @since 2.19
*/
public Stream<Map.Entry<String, JsonNode>> entryStream() {
public Stream<Map.Entry<String, JsonNode>> propertyStream() {
return ClassUtil.emptyStream();
}

/**
* If this node is an {@code ObjectNode}, erforms the given action for each entry
* If this node is an {@code ObjectNode}, performs the given action for each
* property (key, value pair)
* until all entries have been processed or the action throws an exception.
* Exceptions thrown by the action are relayed to the caller.
* For other node types, no action is performed.
*<p>
* Actions are performed in the order of entries, same as order returned by
* Actions are performed in the order of properties, same as order returned by
* method {@link #properties()}.
* This is generally the document order of properties in JSON object.
*
* @param action Action to perform for each entry
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected void _serializeNonRecursive(JsonGenerator g, JsonNode node)
{
if (node instanceof ObjectNode) {
g.writeStartObject(this, node.size());
_serializeNonRecursive(g, new IteratorStack(), node.fields());
_serializeNonRecursive(g, new IteratorStack(), node.properties().iterator());
} else if (node instanceof ArrayNode) {
g.writeStartArray(this, node.size());
_serializeNonRecursive(g, new IteratorStack(), node.values());
Expand Down Expand Up @@ -100,7 +100,7 @@ protected void _serializeNonRecursive(JsonGenerator g, IteratorStack stack,
}
if (value instanceof ObjectNode) {
stack.push(currIt);
currIt = value.fields();
currIt = value.properties().iterator();
g.writeStartObject(value, value.size());
} else if (value instanceof ArrayNode) {
stack.push(currIt);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tools/jackson/databind/node/NodeCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected final static class ObjectCursor
public ObjectCursor(JsonNode n, NodeCursor p)
{
super(TokenStreamContext.TYPE_OBJECT, p);
_contents = n.fields();
_contents = n.properties().iterator();
_needEntry = true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/tools/jackson/databind/node/ObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ public JsonNode required(String propertyName) {
/**
* Method to use for accessing all properties (with both names
* and values) of this JSON Object.
*
* @deprecated since 2.19 Use instead {@link #properties()}.
*/
@Deprecated // since 2.19
@Override
public Iterator<Map.Entry<String, JsonNode>> fields() {
return _children.entrySet().iterator();
Expand Down Expand Up @@ -313,7 +316,7 @@ public Stream<JsonNode> valueStream() {
}

@Override // @since 2.19
public Stream<Map.Entry<String, JsonNode>> entryStream() {
public Stream<Map.Entry<String, JsonNode>> propertyStream() {
return _children.entrySet().stream();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ protected void _depositSchemaProperty(ObjectNode propertiesNode, JsonNode schema
{
JsonNode props = schemaNode.get("properties");
if (props != null) {
Iterator<Entry<String, JsonNode>> it = props.fields();
while (it.hasNext()) {
Entry<String,JsonNode> entry = it.next();
for (Entry<String,JsonNode> entry : props.properties()) {
String name = entry.getKey();
if (_nameTransformer != null) {
name = _nameTransformer.transform(name);
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/tools/jackson/databind/ObjectReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Map.Entry;

import org.junit.jupiter.api.Test;

Expand All @@ -21,12 +20,11 @@
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.jsontype.TypeSerializer;
import tools.jackson.databind.node.*;
import tools.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.*;

import static tools.jackson.databind.testutil.DatabindTestUtil.*;

public class ObjectReaderTest
public class ObjectReaderTest extends DatabindTestUtil
{
private final ObjectMapper MAPPER = newJsonMapper();

Expand Down Expand Up @@ -515,7 +513,7 @@ static class CustomObjectNode extends BaseJsonNode
private final ObjectNode _delegate;

CustomObjectNode(ObjectNode delegate) {
this._delegate = delegate;
_delegate = delegate;
}

@Override
Expand All @@ -529,8 +527,8 @@ public int size() {
}

@Override
public Iterator<Entry<String, JsonNode>> fields() {
return _delegate.fields();
public Set<Map.Entry<String, JsonNode>> properties() {
return _delegate.properties();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/tools/jackson/databind/node/ArrayNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ public void testStreamMethods()
arr.valueStream().collect(Collectors.toList()));

// And then entryStream() (empty)
assertEquals(0, arr.entryStream().count());
assertEquals(0, arr.propertyStream().count());
assertEquals(Arrays.asList(),
arr.entryStream().collect(Collectors.toList()));
arr.propertyStream().collect(Collectors.toList()));

// And then empty forEachEntry()
arr.forEachEntry((k, v) -> { throw new UnsupportedOperationException(); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void assertNodeNumbers(JsonNode n, int expInt, double expDouble)
protected void assertNonContainerStreamMethods(ValueNode n)
{
assertEquals(0, n.valueStream().count());
assertEquals(0, n.entryStream().count());
assertEquals(0, n.propertyStream().count());

// And then empty forEachEntry()
n.forEachEntry((k, v) -> { throw new UnsupportedOperationException(); });
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/tools/jackson/databind/node/ObjectNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testSimpleObject() throws Exception

// Ok, then, let's traverse via extended interface
ObjectNode obNode = (ObjectNode) root;
Iterator<Map.Entry<String,JsonNode>> fit = obNode.fields();
Iterator<Map.Entry<String,JsonNode>> fit = obNode.properties().iterator();
// we also know that LinkedHashMap is used, i.e. order preserved
assertTrue(fit.hasNext());
Map.Entry<String,JsonNode> en = fit.next();
Expand Down Expand Up @@ -144,7 +144,7 @@ public void testBasics()
assertTrue(n.isEmpty());

assertFalse(n.values().hasNext());
assertFalse(n.fields().hasNext());
assertTrue(n.properties().isEmpty());
assertFalse(n.propertyNames().hasNext());
assertNull(n.get("a"));
assertTrue(n.path("a").isMissingNode());
Expand All @@ -154,7 +154,7 @@ public void testBasics()

assertEquals(1, n.size());
assertTrue(n.values().hasNext());
assertTrue(n.fields().hasNext());
assertFalse(n.properties().isEmpty());
assertTrue(n.propertyNames().hasNext());
assertSame(text, n.get("a"));
assertSame(text, n.path("a"));
Expand Down Expand Up @@ -554,9 +554,9 @@ public void testStreamMethods()
obj.valueStream().collect(Collectors.toList()));

// And then entryStream() (empty)
assertEquals(2, obj.entryStream().count());
assertEquals(2, obj.propertyStream().count());
assertEquals(new ArrayList<>(obj.properties()),
obj.entryStream().collect(Collectors.toList()));
obj.propertyStream().collect(Collectors.toList()));

// And then empty forEachEntry()
final LinkedHashMap<String,JsonNode> map = new LinkedHashMap<>();
Expand Down

0 comments on commit 3986751

Please sign in to comment.