Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Un-deprecate JsonNode.asText(defaultValue) #4504

Merged
merged 4 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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)
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/fasterxml/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
*<p>
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public String asText() {
}

@Override
@Deprecated // since 2.17
public String asText(String defaultValue) {
return (_value == null) ? defaultValue : _value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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);

Expand Down