Affects: \5.2.5 Enhancement

  • There is a method Jackson2ObjectMapperBuilder.serializationInclusion(JsonInclude.Include serializationInclusion) which is an equivalent to Jackson's ObjectMapper setSerializationInclusion(JsonInclude.Include incl)
  • Unfortunately there is no equivalent to ObjectMapper.setPropertyInclusion(JsonInclude.Value incl) which allows more elaborate configuration.

Suggestion #1

  • Replace (or add) Jackson2ObjectMapperBuilder.serializationInclusion(JsonInclude.Include serializationInclusion) with Jackson2ObjectMapperBuilder.serializationInclusion(JsonInclude.Value serializationInclusion) which covers more cases.

Suggestion #2

  • Add an object mapper post-processor to Jackson2ObjectMapperBuilder which would allow to perform exotic configurations not supported by builder by default.
  • This would remove the need of extending builder class which comes handy in Spring Boot applications.
//example
class Jackson2ObjectMapperBuilder {

    private Consumer<ObjectMapper> postProcessor;

    public void configure(ObjectMapper objectMapper) {
        // ...
        if (postProcessor != null) {
            postProcessor.accept(objectMapper);
        }
    }

Comment From: bclozel

Hello @vkuzel ,

Jackson2ObjectMapperBuilder.serializationInclusion(JsonInclude.Include serializationInclusion) is mirroring ObjectMapper. setSerializationInclusion(JsonInclude.Include serializationInclusion). Other ObjectMapper methods do accept JsonInclude.Value types as parameters.

Could you take a step back and give an example (ideally a code snippet) of what you're trying to achieve?

Spring Boot already provides several ways to customize ObjectMapper instances and we'd like to remain consistent here.

Thanks!

Comment From: vkuzel

Hi @bclozel

ad "serlization inclusion method") There is a ObjectMapper.setDefaultPropertyInclusion(JsonInclude.Value incl) which should be mirrored. This means builder should have something like Jackson2ObjectMapperBuilder.defaultPropertyInclusion(JsonInclude.Value incl). I am talking about Jackson 2.10.x currently used by Spring.

My goal is to do following configuration via builder.

objectMapper.setDefaultPropertyInclusion(JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.ALWAYS))

ad "flexible solution to configure object mapper") It seems like ObjectMapper's API is being developed faster than builder's API. Suggestion #2 is a way to allow developers to use ObjectMapper's new features that are not implemented in builder's API.

Thank you.

Comment From: bclozel

Given we've got this issue, and #23017 as well - I'm wondering if we should instead follow a Jackson2ObjectMapperBuilder.configure(Consumer<ObjectMapper>) pattern.

Jackson seems to add more options, and adding all options might become brittle if we want to support several Jackson generations.

What do you think @jhoeller @sdeleuze ?

Comment From: rstoyanchev

I think having Consumer<ObjectMapper> for more advanced customizations is a good idea.

Comment From: rstoyanchev

I've added both serializationInclusion(JsonInclude.Value) and a Consumer<ObjectMapper> option.