Back to Lombok

Jacksonized

website/templates/features/experimental/Jacksonized.html

1.18.463.4 KB
Original Source

#import "../_features.html" as f> <@f.scaffold title="@Jacksonized" logline="Bob, meet Jackson. Lets make sure you become fast friends."> <@f.history>

@Jacksonized was introduced as experimental feature for @Builder and @SuperBuilder in lombok v1.18.14.

Support for @Accessors was added in lombok v1.18.40.

@f.history> <@f.overview>

The @Jacksonized annotation is an add-on annotation for @Builder, @SuperBuilder, and @Accessors.

For @Accessors(fluent = true) on a type, it automatically configures Jackson to use the generated setters and getters for (de-)serialization by inserting @JsonProperty annotations. (Note that @Jacksonized @Accessors on fields are not supported.)

@Jacksonized @Accessors(fluent = true)
@Getter @Setter
public class AccessorsExample {
	private int age = 10;
}

For @Builder and @SuperBuilder, it automatically configures the generated builder class to be used by Jackson's deserialization. It only has an effect if present at a context where there is also a @Builder or a @SuperBuilder; a warning is emitted otherwise.

Without @Jacksonized, you would have to customize your builder class(es). With @Jacksonized, you can simply write something like this to let Jackson use the generated builder:

@Jacksonized @Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class JacksonExample {
	private List<Foo> foos;
}

This annotation does not change the behavior of the generated builder. A @Jacksonized @SuperBuilder remains fully compatible to regular @SuperBuilders.

@f.overview> <@f.confKeys> lombok.jacksonized.flagUsage = [warning | error] (default: not set) Lombok will flag any usage of @Jacksonized as a warning or error if configured. lombok.jacksonized.jacksonVersion += version (default: 2 + generate warning) (Since 1.18.44) Jackson now has 2 different major versions: Jackson2, and Jackson3. Specify which version of the jackson annotations to generate.
If no version is chosen, lombok will generate Jackson v2 annotations and emit a warning that you should add this config key to pick which jackson version you want.
If transitioning from Jackson2 to Jackson3, you can ask lombok to emit both variants. See: Lombok configuration system. @f.confKeys> <@f.smallPrint>

In particular, the annotation does the following for @(Super)Builder:

  • Configure Jackson to use the builder for deserialization using @JsonDeserialize(builder=Foobar.FoobarBuilder[Impl].class)) on the class (where Foobar is the name of the annotated class, and Impl is added for @SuperBuilder). (An error is emitted if such an annotation already exists.)
  • Copy Jackson-related configuration annotations (like @JsonIgnoreProperties) from the class to the builder class. This is necessary so that Jackson recognizes them when using the builder.
  • Insert @JsonPOJOBuilder(withPrefix="") on the generated builder class to override Jackson's default prefix "with". If you configured a different prefix in lombok using setterPrefix, this value is used. If you changed the name of the build() method using buildMethodName, this is also made known to Jackson.
  • For @SuperBuilder, make the builder implementation class package-private.

@f.smallPrint> @f.scaffold>