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

Enable default values for text nodes #463

Closed
ccleve opened this issue May 19, 2014 · 5 comments
Closed

Enable default values for text nodes #463

ccleve opened this issue May 19, 2014 · 5 comments

Comments

@ccleve
Copy link

ccleve commented May 19, 2014

When a JsonNode contains an integer or boolean, there's a nice option to specify a default value:

int myInt = myNode.asInt(defaultValue);

This doesn't exist for text nodes. It would be nice to have this:

String myString = myNode.asText("a default value here");

@cowtowncoder
Copy link
Member

Sounds reasonable to me.

One question is as to when this should be used: with primitives, value is used if no coercion is found. But with Strings, most other types would convert to String. So should this only use default value if property is missing altogether? Or if it does not have scalar value (i.e. missing property, as well as array/object valued, would use default).

@ccleve
Copy link
Author

ccleve commented May 20, 2014

My thought was that the default value should be returned only when the
value is missing. Otherwise, getText() should behave normally.

On 5/19/2014 6:59 PM, Tatu Saloranta wrote:

Sounds reasonable to me.

One question is as to when this should be used: with primitives, value
is used if no coercion is found. But with Strings, most other types
would convert to String. So should this only use default value if
property is missing altogether? Or if it does not have scalar value
(i.e. missing property, as well as array/object valued, would use
default).


Reply to this email directly or view it on GitHub
#463 (comment).

Chris Cleveland
Dieselpoint, Inc.
117 N Jefferson Street, Suite 202
Chicago, Illinois 60661
+1 773.528.1700 x116
+1 312.339.2677 mobile
http://dieselpoint.com
[email protected]

This email and any attachments contain information from Dieselpoint,
Inc. and should be considered confidential. If this email is
received in error, please delete it and notify the sender.

@cowtowncoder cowtowncoder added this to the 1.9.13 milestone May 21, 2014
cowtowncoder added a commit that referenced this issue May 21, 2014
@cowtowncoder
Copy link
Member

Implemented: will return defaultValue for these specific cases:

  • For MissingNode (virtual node returned with "path()", and "at(JSONPointer)")
  • For 'NullNode`
  • For other node types that can contain null value (POJONode, possible StringNode)

but note that it is NOT returned if empty String ("") is available.

@henning-meinhardt
Copy link

henning-meinhardt commented Apr 23, 2024

Hi, bit late to the party but anyway: after upgrading to the latest Jackson databind release I noticed a deprecation warning when using .asText (defaultValue) but don't really understand the explanation given in the java doc:

    /**
     * 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.
     *<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()}.
     *
     * @since 2.4
     *
     * @deprecated Since 2.17, to be removed from 3.0
     */
    @Deprecated // @since 2.17
    public String asText(String defaultValue) {

It says that almost no node type did ever return null so the == null comparison was meaningless most of the time. That might be right but doesn't legitimate the deprecation. MissingNode subclass overwrites asText (defaultValue) as follows:

@Override public String asText(String defaultValue) { return defaultValue; }

So the == null check is actually not involved and a MissingNode always returned the specified default value.
So I could use something like this to default a missing node to a meaningful default value:

JsonNode node = ... {} // an empty node {} for example
String value = node.path ("property").asText ("default value");

That returned "default value" for an empty JsonNode.
Now that its deprecated I would have to write more complicated code (or there is a shorter replacement that is unfortunately not mentioned in the deprecation comment):

JsonNode node = ... {} // an empty node {} for example
String value = node.path ("property").getText ();
if (value == null)
    value = "defaultValue";

or

JsonNode node = ... {} // an empty node {} for example
JSonNode propertyNode = node.path ("property");
String value = propertyNode.isMissingNode () ? "defaultValue" : propertyNode.getText();

I have two questions:

  1. Is there a simpler workaround for the now missing defaultValue feature?
  2. Why did you deprecate asText(defaultValue) but left asInt(defaultValue), asLong(defaultValue) and so on?

Kind regards
Henning

@cowtowncoder
Copy link
Member

Follow-up discussion on #4471.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants