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 Dec 30, 2024
2 parents 5c6ec83 + 3e0d39b commit 9fc2bf2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Project: jackson-databind
(reported by Kornel Zemla)
#4863: Add basic Stream support in `JsonNode`: `valueStream()`, `propertyStream()`,
`forEachEntry()`
#4867: Add `Optional<JsonNode> JsonNode.asOptional()` convenience method
(fix by Joo-Hyuk K)

2.18.3 (not yet released)

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/tools/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,17 @@ public boolean asBoolean(boolean defaultValue) {
return defaultValue;
}

/**
* Method that will return a {@link JsonNode} wrapped in Java's {@link Optional}.
*
* @return `Optional<JsonNode>` containing this node, or {@link Optional#empty()}
* if this is a {@link MissingNode}.
* @since 2.19
*/
public Optional<JsonNode> asOptional() {
return Optional.of(this);
}

/*
/**********************************************************************
/* Public API, extended traversal (2.10) with "required()"
Expand Down
52 changes: 32 additions & 20 deletions src/main/java/tools/jackson/databind/node/MissingNode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.jackson.databind.node;

import java.util.List;
import java.util.Optional;

import tools.jackson.core.*;
import tools.jackson.databind.JsonNode;
Expand Down Expand Up @@ -65,24 +66,8 @@ public final boolean isMissingNode() {
*/

@Override
public final void serialize(JsonGenerator g, SerializationContext provider)
throws JacksonException
{
/* Nothing to output... should we signal an error tho?
* Chances are, this is an erroneous call. For now, let's
* not do that; serialize as explicit null. Why? Because we
* cannot just omit a value as JSON Object field name may have
* been written out.
*/
g.writeNull();
}

@Override
public void serializeWithType(JsonGenerator g, SerializationContext provider,
TypeSerializer typeSer)
throws JacksonException
{
g.writeNull();
public Optional<JsonNode> asOptional() {
return Optional.empty();
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -139,9 +124,36 @@ public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar) {
}

/*
/**********************************************************
/**********************************************************************
/* Serialization: bit tricky as we don't really have a value
/**********************************************************************
*/

@Override
public final void serialize(JsonGenerator g, SerializationContext provider)
throws JacksonException
{
/* Nothing to output... should we signal an error tho?
* Chances are, this is an erroneous call. For now, let's
* not do that; serialize as explicit null. Why? Because we
* cannot just omit a value as JSON Object field name may have
* been written out.
*/
g.writeNull();
}

@Override
public void serializeWithType(JsonGenerator g, SerializationContext provider,
TypeSerializer typeSer)
throws JacksonException
{
g.writeNull();
}

/*
/**********************************************************************
/* Standard method overrides
/**********************************************************
/**********************************************************************
*/

@Override
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/tools/jackson/databind/node/JsonNodeBasicTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.jackson.databind.node;

import java.util.Comparator;
import java.util.Optional;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -214,4 +215,37 @@ public void testArrayWithDefaultTyping() throws Exception
assertEquals(1, obj.size());
assertEquals(2, obj.path("a").asInt());
}

// [databind#4867]
@Test
public void testAsOptional() {
// Test with MissingNode
JsonNode missingNode = MissingNode.getInstance();
Optional<JsonNode> missingOptional = missingNode.asOptional();
assertFalse(missingOptional.isPresent());

// Test with ObjectNode
ObjectNode objectNode = MAPPER.createObjectNode();
Optional<JsonNode> objectOptional = objectNode.asOptional();
assertTrue(objectOptional.isPresent());
assertEquals(objectNode, objectOptional.get());

// Test with ArrayNode
ArrayNode arrayNode = MAPPER.createArrayNode();
Optional<JsonNode> arrayOptional = arrayNode.asOptional();
assertTrue(arrayOptional.isPresent());
assertEquals(arrayNode, arrayOptional.get());

// Test with TextNode
TextNode textNode = TextNode.valueOf("text");
Optional<JsonNode> textOptional = textNode.asOptional();
assertTrue(textOptional.isPresent());
assertEquals(textNode, textOptional.get());

// Test with NullNode
NullNode nullNode = NullNode.getInstance();
Optional<JsonNode> nullOptional = nullNode.asOptional();
assertTrue(nullOptional.isPresent());
assertEquals(nullNode, nullOptional.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void testMissingViaMapper() throws Exception
assertTrue(onode.isObject());
assertEquals(0, onode.size());
assertFalse(onode.isMissingNode()); // real node
assertTrue(onode.asOptional().isPresent());
assertNull(onode.textValue());

// how about dereferencing?
Expand All @@ -74,6 +75,7 @@ public void testMissingViaMapper() throws Exception
JsonNode dummyNode2 = dummyNode.path(98);
assertNotNull(dummyNode2);
assertTrue(dummyNode2.isMissingNode());
assertFalse(dummyNode2.asOptional().isPresent());
JsonNode dummyNode3 = dummyNode.path("field");
assertNotNull(dummyNode3);
assertTrue(dummyNode3.isMissingNode());
Expand All @@ -92,6 +94,7 @@ public void testMissingViaMapper() throws Exception
assertTrue(dummyNode.isMissingNode());
assertNull(dummyNode.get(0));
assertNull(dummyNode.get("myfield"));
assertFalse(dummyNode.asOptional().isPresent());
dummyNode2 = dummyNode.path(98);
assertNotNull(dummyNode2);
assertTrue(dummyNode2.isMissingNode());
Expand Down

0 comments on commit 9fc2bf2

Please sign in to comment.