jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Add callback to signal null for primitive in DeserializationProblemHandler

Open jnizet opened this issue 2 weeks ago • 1 comments

Is your feature request related to a problem? Please describe.

See #5451

Describe the solution you'd like

I'd like to be able to configure a JsonMapper with a DeserializationProblemHandler that would allow me to log when a null has been met where a primitive is required. The new method of the DeserializationProblemHandler would allow me to log (either through separate arguments of the method, or with a ready to log message)

  • which type is expected (example: double)
  • at which location (example: com.foo.bar.Coordinates#latitude)

Usage example

var mapper = JsonMapper.builder().addHandler(new DeserializationProblemHandler() {
    @Override
    public void handleNullForPrimitive(String message) {
        logger.warn("This problem should be handled before turning FAIL_ON_NULL_FOR_PRIMITIVES: " + message);                
    }
}).build();

And if a null is found for a primitive, that would thus log something like

This problem should be handled before turning FAIL_ON_NULL_FOR_PRIMITIVES: Met null for primitive of type `int` in com.foo.bar.Coordinates#latitude

The logger could create a log an exception to have the stack trace allowing to have more context (i.e. where the deserialization happened).

Additional context

No response

jnizet avatar Dec 04 '25 16:12 jnizet

Quick note: all callbacks in DeserializationProblemHandler need to contain enough metadata for actual handling, and have proper return value to be able to do so. But that can be determined by anyone working on this issue. So it would not be designed for logging, but would be able to also be used for that.

cowtowncoder avatar Dec 04 '25 17:12 cowtowncoder

Fixed via #5493 which adds DeserializationProblemHandler.handleNullForPrimitives() callback.

cowtowncoder avatar Dec 16 '25 02:12 cowtowncoder

@jnizet for usage u could check tests from the PR

JooHyukKim avatar Dec 16 '25 11:12 JooHyukKim