Skip to content

Extend, improve set of JsonNode.asXxx() methods for number types [JSTEP-3] #5003

@cowtowncoder

Description

@cowtowncoder

(note: continuation of #4958 and #4991 )

Jackson 2.x has methods like JsonNode.asInt() to allow some additional coercions from types not directly compatible (int only coercible from JSON Numbers):

  • asInt() similar to intValue() but allows coercion from (compatible) String, as well as null (and even Boolean?). But if not, throw exception
  • asIntOpt() like asInt() but returns OptionalInt, either present (as per asInt()) or absent (instead of exception)

but as with JsonNode.xxxValue() methods like intValue(), no exception is thrown but quietly default value (like 0 for int) is returned. For Jackson 3.0 we have unchecked JsonNodeException so use that instead.
We can also clarify coercions that are allowed, as well as extend set of asXxx() methods as it makes sense.

Types/methods to handle:

  • asDecimal() (BigDecimal)
  • asDouble()
  • asInt()
  • asLong()

but let's also add

  • asBigInteger()

(probably not asFloat() or asShort() or asByte())

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.0Issue planned for initial 3.0 release

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions