Skip to content

NullNode#asString and NullNode#asString(String) discrepency with Jackson 2 #5417

@filiphr

Description

@filiphr

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

There is a discrepency between the asString behavior of NullNode on Jackson 3 and Jackson 2.

Version Information

3.0.1 and 2.20.1

Reproduction

If we take:

System.out.println(NullNode.getInstance().asText());
System.out.println(NullNode.getInstance().asText("default"));

Expected behavior

With Jackson 2 it prints:

null
default

With Jackson 3 it prints:



i.e. both methods return an empty String on Jackson 3. However, with Jackson 2

Additional context

I did find #5034, but I could not understand the reasoning for the change of the asString(String). For asString() you can argue that maybe an empty string is fine, but why empty string for asString(String)? I also did see #5287 which makes stringValue(String) return the default value for null nodes.

We have a lot of parsing code that uses JsonNode and we use a lot object.path("someProperty").asText("default") to get a value for it. The pattern for this now should be object.path("someProperty").stringValue("default"), but this is something that did not exist in Jackson 2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    to-evaluateIssue that has been received but not yet evaluated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions