diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index fc57d5d8ca..3dfbaf8e6e 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -41,6 +41,9 @@ Project: jackson-databind (fix by Joo-Hyuk K) #4450: Empty QName deserialized as `null` (reported by @winfriedgerlach) +#4471: Reconsider deprecation of `JsonNode.asText(defaultValue)` + (requested by @aerisnju) + (fix by Joo-Hyuk K) #4481: Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL` with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL` (reported by @luozhenyu) diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonNode.java b/src/main/java/com/fasterxml/jackson/databind/JsonNode.java index e2f813bcd9..67e42ca149 100644 --- a/src/main/java/com/fasterxml/jackson/databind/JsonNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/JsonNode.java @@ -636,16 +636,12 @@ public byte[] binaryValue() throws IOException { * {@link com.fasterxml.jackson.databind.node.NullNode}, ensuring a default value is returned instead of null or missing indicators. * *

- * NOTE: deprecated since 2.17 because {@link #asText()} very rarely returns - * {@code null} for any node types -- in fact, neither {@link MissingNode} - * nor {@code NullNode} return {@code null} from {@link #asText()}. + * NOTE: This was deprecated in 2.17.0, but as discussed through [databind#4471], was un-deprecated in 2.17.1. * * @param defaultValue The default value to return if this node's text value is absent. * @return The text value of this node, or defaultValue if the text value is absent. * @since 2.4 - * @deprecated Since 2.17, to be removed from 3.0 */ - @Deprecated // @since 2.17 public String asText(String defaultValue) { String str = asText(); return (str == null) ? defaultValue : str; diff --git a/src/main/java/com/fasterxml/jackson/databind/node/MissingNode.java b/src/main/java/com/fasterxml/jackson/databind/node/MissingNode.java index 87843c274b..fcaf5823dd 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/MissingNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/MissingNode.java @@ -60,7 +60,6 @@ public JsonNodeType getNodeType() @Override public String asText() { return ""; } - @Deprecated // since 2.17 @Override public String asText(String defaultValue) { return defaultValue; } // // Note: not a numeric node, hence default 'asXxx()' are fine: diff --git a/src/main/java/com/fasterxml/jackson/databind/node/NullNode.java b/src/main/java/com/fasterxml/jackson/databind/node/NullNode.java index 822cbb2040..be67b7e6f6 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/NullNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/NullNode.java @@ -41,7 +41,6 @@ public JsonNodeType getNodeType() { @Override public JsonToken asToken() { return JsonToken.VALUE_NULL; } @Override - @Deprecated public String asText(String defaultValue) { return defaultValue; } @Override public String asText() { return "null"; } diff --git a/src/main/java/com/fasterxml/jackson/databind/node/POJONode.java b/src/main/java/com/fasterxml/jackson/databind/node/POJONode.java index b24511bb5e..ad411edc08 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/POJONode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/POJONode.java @@ -58,7 +58,6 @@ public byte[] binaryValue() throws IOException public String asText() { return (_value == null) ? "null" : _value.toString(); } @Override - @Deprecated public String asText(String defaultValue) { return (_value == null) ? defaultValue : _value.toString(); } diff --git a/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java b/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java index d22ba33541..83d256fe70 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java @@ -102,7 +102,6 @@ public String asText() { } @Override - @Deprecated // since 2.17 public String asText(String defaultValue) { return (_value == null) ? defaultValue : _value; } diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/CustomDeserializersTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/CustomDeserializersTest.java index c2bfe713ea..1f2982a8a6 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/CustomDeserializersTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/CustomDeserializersTest.java @@ -355,7 +355,7 @@ public NamedPoint deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JsonNode tree = ctxt.readTree(p); - String name = tree.path("name").asText(); + String name = tree.path("name").asText(null); Point point = ctxt.readTreeAsValue(tree.get("point"), Point.class); return new NamedPoint(name, point); } diff --git a/src/test/java/com/fasterxml/jackson/databind/node/MissingNodeTest.java b/src/test/java/com/fasterxml/jackson/databind/node/MissingNodeTest.java index e46f3ab763..2062936d99 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/MissingNodeTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/MissingNodeTest.java @@ -18,6 +18,7 @@ public void testMissing() assertTrue(n.isMissingNode()); assertEquals(JsonToken.NOT_AVAILABLE, n.asToken()); assertEquals("", n.asText()); + assertEquals("default", n.asText("default")); assertStandardEquals(n); // 10-Dec-2018, tatu: With 2.10, should serialize same as via ObjectMapper/ObjectWriter // 10-Dec-2019, tatu: Surprise! No, this is not how it worked in 2.9, nor does it make diff --git a/src/test/java/com/fasterxml/jackson/databind/node/NullNodeTest.java b/src/test/java/com/fasterxml/jackson/databind/node/NullNodeTest.java index d954e09ed4..0141130d1b 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/NullNodeTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/NullNodeTest.java @@ -52,6 +52,9 @@ public void testBasicsWithNullNode() throws Exception assertEquals(0L, n.longValue()); assertEquals(BigDecimal.ZERO, n.decimalValue()); assertEquals(BigInteger.ZERO, n.bigIntegerValue()); + // may be odd but... + assertEquals("null", n.asText()); + assertEquals("fallback", n.asText("fallback")); assertEquals(0, n.size()); assertTrue(n.isEmpty()); diff --git a/src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java b/src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java index 9eb35a222c..07117ddbd9 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java @@ -88,7 +88,7 @@ public void testInt() assertEquals(BigInteger.ONE, n.bigIntegerValue()); assertEquals("1", n.asText()); // 2.4 - assertEquals("1", n.asText()); + assertEquals("1", n.asText("foo")); assertNodeNumbers(n, 1, 1.0);