Skip to content

Commit

Permalink
Fix #3809: Add ObjectNode.properties()
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 18, 2023
1 parent 291720e commit a3ded7b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Project: jackson-databind
(reported by Christoph S)
#3796: Enum Deserialisation Failing with Polymorphic type validator
(reported by @sagarika4)
#3809: Add Stream-friendly alternative to `ObjectNode.fields()`:
`Set<Map.Entry<String, JsonNode>> properties()`
#3816: TokenBuffer does not implement writeString(Reader reader, int len)
(reported by Patrick S)
#3819: Add convenience method `SimpleBeanPropertyFilter.filterOutAll()` as
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,21 @@ public Iterator<Map.Entry<String, JsonNode>> fields() {
return ClassUtil.emptyIterator();
}

/**
* 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}.
*
* @return Set of properties, if this node is an {@code ObjectNode}
* ({@link JsonNode#isObject()} returns {@code true}); empty
* {@link java.util.Set} otherwise.
*
* @since 2.15
*/
public Set<Map.Entry<String, JsonNode>> properties() {
return Collections.emptySet();
}

/*
/**********************************************************
/* Public API, find methods
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ public Iterator<Map.Entry<String, JsonNode>> fields() {
return _children.entrySet().iterator();
}

/**
* Method to use for accessing all properties (with both names
* and values) of this JSON Object.
*
* @since 2.15
*/
@Override
public Set<Map.Entry<String, JsonNode>> properties() {
return _children.entrySet();
}

@Override
public boolean equals(Comparator<JsonNode> comparator, JsonNode o)
{
Expand Down
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.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -32,13 +33,6 @@ public DataImpl(JsonNode n) {

@JsonValue
public JsonNode value() { return root; }

/*
public Wrapper(ObjectNode n) { root = n; }
@JsonValue
public ObjectNode value() { return root; }
*/
}

static class ObNodeWrapper {
Expand Down Expand Up @@ -69,7 +63,7 @@ static class MyValue
/**********************************************************
*/

private final ObjectMapper MAPPER = sharedMapper();
private final ObjectMapper MAPPER = newJsonMapper();

public void testSimpleObject() throws Exception
{
Expand Down Expand Up @@ -509,12 +503,30 @@ public void testIssue941() throws Exception

public void testSimpleMismatch() throws Exception
{
ObjectMapper mapper = objectMapper();
try {
mapper.readValue("[ 1, 2, 3 ]", ObjectNode.class);
MAPPER.readValue("[ 1, 2, 3 ]", ObjectNode.class);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "from Array value (token `JsonToken.START_ARRAY`)");
}
}

// [databind#3809]
public void testPropertiesTraversal() throws Exception
{
// Nothing to traverse for other types
assertEquals("", _toString(MAPPER.createArrayNode()));
assertEquals("", _toString(MAPPER.getNodeFactory().textNode("foo")));

// But yes for ObjectNode:
JsonNode n = MAPPER.readTree(a2q(
"{ 'a':1, 'b':true,'c':'stuff'}"));
assertEquals("a/1,b/true,c/\"stuff\"", _toString(n));
}

private String _toString(JsonNode n) {
return n.properties().stream()
.map(e -> e.getKey() + "/" + e.getValue())
.collect(Collectors.joining(","));
}
}

0 comments on commit a3ded7b

Please sign in to comment.