Skip to content

Commit 517056b

Browse files
committed
Fix #4416: Deprecate JsonNode.asText(String)
1 parent 6360c8f commit 517056b

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -1752,3 +1752,7 @@ Jesper Blomquist (jebl01@github)
17521752
* Contributed #4393: Deserialize `java.util.UUID` encoded as Base64 and base64Url with or
17531753
without padding
17541754
(2.17.0)
1755+
1756+
András Péteri (apeteri@github)
1757+
* Suggested #4416: Deprecate `JsonNode.asText(String)`
1758+
(2.17.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Project: jackson-databind
4545
#4394: Better Base64 support for `java.util.UUIDs`
4646
without padding
4747
(fix contributed by Jesper B)
48+
#4416: Deprecate `JsonNode.asText(String)`
49+
(suggested by András P)
4850
- JUnit5 upgraded to 5.10.1
4951
5052
2.16.2 (not yet released)

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

+7
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,16 @@ public byte[] binaryValue() throws IOException {
635635
* <code>defaultValue</code> in cases where null value would be returned;
636636
* either for missing nodes (trying to access missing property, or element
637637
* at invalid item for array) or explicit nulls.
638+
*<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()}.
638642
*
639643
* @since 2.4
644+
*
645+
* @deprecated Since 2.17, to be removed from 3.0
640646
*/
647+
@Deprecated // @since 2.17
641648
public String asText(String defaultValue) {
642649
String str = asText();
643650
return (str == null) ? defaultValue : str;

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(null);
358+
String name = tree.path("name").asText();
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/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("foo"));
85+
assertEquals("1", n.asText());
8686

8787
assertNodeNumbers(n, 1, 1.0);
8888

0 commit comments

Comments
 (0)