Skip to content

Commit 55b48b6

Browse files
authored
Un-deprecate JsonNode.asText(defaultValue) (#4504)
1 parent e088857 commit 55b48b6

File tree

10 files changed

+10
-11
lines changed

10 files changed

+10
-11
lines changed

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Project: jackson-databind
2525
(fix by Joo-Hyuk K)
2626
#4450: Empty QName deserialized as `null`
2727
(reported by @winfriedgerlach)
28+
#4471: Reconsider deprecation of `JsonNode.asText(defaultValue)`
29+
(requested by @aerisnju)
30+
(fix by Joo-Hyuk K)
2831
#4481: Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
2932
with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
3033
(reported by @luozhenyu)

src/main/java/com/fasterxml/jackson/databind/JsonNode.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -636,16 +636,12 @@ public byte[] binaryValue() throws IOException {
636636
* {@link com.fasterxml.jackson.databind.node.NullNode}, ensuring a default value is returned instead of null or missing indicators.
637637
*
638638
*<p>
639-
* NOTE: deprecated since 2.17 because {@link #asText()} very rarely returns
640-
* {@code null} for any node types -- in fact, neither {@link MissingNode}
641-
* nor {@code NullNode} return {@code null} from {@link #asText()}.
639+
* NOTE: This was deprecated in 2.17.0, but as discussed through [databind#4471], was un-deprecated in 2.17.1.
642640
*
643641
* @param defaultValue The default value to return if this node's text value is absent.
644642
* @return The text value of this node, or defaultValue if the text value is absent.
645643
* @since 2.4
646-
* @deprecated Since 2.17, to be removed from 3.0
647644
*/
648-
@Deprecated // @since 2.17
649645
public String asText(String defaultValue) {
650646
String str = asText();
651647
return (str == null) ? defaultValue : str;

src/main/java/com/fasterxml/jackson/databind/node/MissingNode.java

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public JsonNodeType getNodeType()
6060

6161
@Override public String asText() { return ""; }
6262

63-
@Deprecated // since 2.17
6463
@Override public String asText(String defaultValue) { return defaultValue; }
6564

6665
// // Note: not a numeric node, hence default 'asXxx()' are fine:

src/main/java/com/fasterxml/jackson/databind/node/NullNode.java

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public JsonNodeType getNodeType() {
4141
@Override public JsonToken asToken() { return JsonToken.VALUE_NULL; }
4242

4343
@Override
44-
@Deprecated
4544
public String asText(String defaultValue) { return defaultValue; }
4645

4746
@Override public String asText() { return "null"; }

src/main/java/com/fasterxml/jackson/databind/node/POJONode.java

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public byte[] binaryValue() throws IOException
5858
public String asText() { return (_value == null) ? "null" : _value.toString(); }
5959

6060
@Override
61-
@Deprecated
6261
public String asText(String defaultValue) {
6362
return (_value == null) ? defaultValue : _value.toString();
6463
}

src/main/java/com/fasterxml/jackson/databind/node/TextNode.java

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public String asText() {
102102
}
103103

104104
@Override
105-
@Deprecated // since 2.17
106105
public String asText(String defaultValue) {
107106
return (_value == null) ? defaultValue : _value;
108107
}

src/test/java/com/fasterxml/jackson/databind/deser/CustomDeserializersTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public NamedPoint deserialize(JsonParser p, DeserializationContext ctxt)
355355
throws IOException
356356
{
357357
JsonNode tree = ctxt.readTree(p);
358-
String name = tree.path("name").asText();
358+
String name = tree.path("name").asText(null);
359359
Point point = ctxt.readTreeAsValue(tree.get("point"), Point.class);
360360
return new NamedPoint(name, point);
361361
}

src/test/java/com/fasterxml/jackson/databind/node/MissingNodeTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public void testMissing()
1414
assertTrue(n.isMissingNode());
1515
assertEquals(JsonToken.NOT_AVAILABLE, n.asToken());
1616
assertEquals("", n.asText());
17+
assertEquals("default", n.asText("default"));
1718
assertStandardEquals(n);
1819
// 10-Dec-2018, tatu: With 2.10, should serialize same as via ObjectMapper/ObjectWriter
1920
// 10-Dec-2019, tatu: Surprise! No, this is not how it worked in 2.9, nor does it make

src/test/java/com/fasterxml/jackson/databind/node/NullNodeTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public void testBasicsWithNullNode() throws Exception
4848
assertEquals(0L, n.longValue());
4949
assertEquals(BigDecimal.ZERO, n.decimalValue());
5050
assertEquals(BigInteger.ZERO, n.bigIntegerValue());
51+
// may be odd but...
52+
assertEquals("null", n.asText());
53+
assertEquals("fallback", n.asText("fallback"));
5154

5255
assertEquals(0, n.size());
5356
assertTrue(n.isEmpty());

src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void testInt()
8282
assertEquals(BigInteger.ONE, n.bigIntegerValue());
8383
assertEquals("1", n.asText());
8484
// 2.4
85-
assertEquals("1", n.asText());
85+
assertEquals("1", n.asText("foo"));
8686

8787
assertNodeNumbers(n, 1, 1.0);
8888

0 commit comments

Comments
 (0)