Skip to content

Deprecate JsonNode.asText(String) (note: reverted in 2.17.1) #4416

@apeteri

Description

@apeteri

NOTE: see #4471 for current state -- this was done for 2.17.0 release but reverted for 2.17.1


Describe the bug

It looks like the fallback value in JsonNode#asText(String) never gets a chance to appear as the 0-arg asText() should never return null, according to its Javadoc:

/**
* Method that will return a valid String representation of
* the container value, if the node is a value node
* (method {@link #isValueNode} returns true),
* otherwise empty String.
*/
public abstract String asText();
/**
* Method similar to {@link #asText()}, except that it will return
* <code>defaultValue</code> in cases where null value would be returned;
* either for missing nodes (trying to access missing property, or element
* at invalid item for array) or explicit nulls.
*/
public String asText(String defaultValue) {
String str = asText();
return (str == null) ? defaultValue : str;
}

This seems to be the case in all implementations of asText() I can see, and has been corroborated in other issues such as #25.

The only exception is if someone calls TextNode(String) directly with null as the input. TextNode#valueOf(String) already converts these cases into null references, so the "almost" in the title is quite strong.

Version Information

2.16.1

Expected behavior

Either replace the null check with an emptyness check (but then "legitimate empty" strings will also use the default value), or deprecate the method? Not quite sure!

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.17Issues planned at earliest for 2.17

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions