Search code examples
javajacksonimmutabilityimmutables-library

How to use Jackson PropertyNamingStrategy with Immutables library


I am using https://immutables.github.io/ library with Jackson. I want my class to be Jackson-serializable. I would like to use custom PropertyNamingStrategy (configured for com.fasterxml.jackson.databind.ObjectMapper using mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);

Unfortunately Immutables library puts @JsonProperty("propertyName") at every field in generated code. That overrides PropertyNamingStrategy defined at mapper level (or at class level, using @com.fasterxml.jackson.databind.annotation.JsonNaming annotation).

Is it possible to make Immutables library stop putting property name for every field (using org.immutables.value.Value.Style or similar means)?

I have come with workaround, putting @JsonProperty (without value) at every property, but I am not satisfied with this.


Solution

  • Style property forceJacksonPropertyNames=false does not force property names to specific strings. It works with naming strategies configured at class level and at mapper level.

    Introduction to styles is available here: http://immutables.github.io/style.html

    You can read some background information about this setting in issues: https://github.com/immutables/immutables/issues/431 https://github.com/immutables/immutables/issues/353