Skip to content

Commit

Permalink
Add Optional<JsonNode> JsonNode.asOptional() convenience method for #…
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Dec 30, 2024
1 parent bdd667e commit 3e0d39b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 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/com/fasterxml/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.node;

import java.io.IOException;
import java.util.Optional;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -71,6 +72,11 @@ public JsonNodeType getNodeType()
public boolean asBoolean(boolean defaultValue);
*/

@Override
public Optional<JsonNode> asOptional() {
return Optional.empty();
}

/*
/**********************************************************
/* Serialization: bit tricky as we don't really have a value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.node;

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

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -215,4 +216,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 3e0d39b

Please sign in to comment.