Skip to content

Change defaulting of JsonNode.asXxx(defaultValue)/JsonNode.asXxxOpt() for NullNode #5558

@cowtowncoder

Description

@cowtowncoder

(note: result of #5417)

Jackson 3.0 changed coercion behavior for existing 2.x methods such as asString(String defValue) for NullNodes (mapped from explicit JSON null values) so that instead of returning defValue, they coerce into "empty" value of type -- in case of asString(), empty String "". In 2.x, matching method (named asText()) returned defValue.
Similarly new Optional<String> asStringOpt() returns new Optional<String>("") instead Optional.empty().
This applies consistently across all various asXxx() and asXxxOpt() methods, for NullNodes.

Note: problem does not affect MissingNode. Also, plain asString() coerces NullNode into "", and this is expected due to lack of defaulting.

Based on Strong developer feedback, we should change the behavior of NullNode so that following is true:

  • asXxx() will coerce into default/empty value of type Xxx: "" for Strings, 0 for Ints, false for Boolean and so on. THIS IS EXISTING BEHAVIOR of 3.0 (no change)
  • asXxx(Xxx defValue) will simply return defValue. THIS IS CHANGING
  • asXxxOpt() will simply return "missing" optional: Optional.empty(), OptionalInt.empty() and so on. THIS IS CHANGING

Note: no changes suggested for behavior of non-coercing accessors (xxxValue(), xxxValue(defValue), xxxValueOpt() -- stringValue(), stringValue(String defValue), Optional<String> stringValueOpt()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions