question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow overriding builder's "withPrefix" with new `@JsonDeserialize` property

See original GitHub issue

It would be nice to be able to configure the builder method name prefix Jackson uses either globally through a configuration on ObjectMapper or through the @JsonDeserialize annotation itself. I’m currently using Lombok to generate my builders for me and to make it work nicely with Jackson I have to write boilerplate code, which is the whole point of lombok.

The Problem

Currently, in order to use Jackson and lombok in tandem, I have to write a model class like this:

@Builder
@JsonDeserialize(builder = Model.ModelBuilder.class)
public class Model {
	
	/** various properties */

	@JsonPOJOBuilder(withPrefix = "")
	public static class ModelBuilder {
	}
}

The issue becomes even worse if using the @SuperBuilder annotation:

@SuperBuilder
@JsonDeserialize(builder = Model.ModelBuilderImpl.class)
public class Model extends AbstractModel {

	/** various properties */

	@JsonPOJOBuilder(withPrefix = "")
	protected static final class ModelBuilderImpl extends Model.ModelBuilder<Model, Model.ModelBuilderImpl> {
	}
}

There’s also a blurb about the workaround in Lombok’s own documentation.

Related issue: https://github.com/FasterXML/jackson-databind/issues/1997

Global Config

One way to fix it would be to allow this to be configured globally on ObjectMapper like:

objectMapper.configure(DeserializationFeature.POJO_BUILDER_PREFIX, "");

JsonDeserialize Property

Another solution would be to allow it to be set on the @JsonDeserialize annotation itself, like:

@JsonDeserialize(builder = Model.ModelBuilder.class, withBuilderPrefix = "")

Personally, I prefer option 2.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Aug 29, 2020

Ok, with #2800, there now IS a way to, I think, configure builder-“with” prefix, using new AccessNamingStrategy (and provider). And in fact DefaultAccessorNamingStrategy allows configuration, so something like:

    ObjectMapper mapper = JsonMapper.builder()
                .accessorNaming(new DefaultAccessorNamingStrategy.Provider()
                        .withBuilderPrefix("") // no prefix
// or                       .withBuilderPrefix("With") //  would expect 'WithX()' etc
                )
                .build();

or, if you want something more complicated (multiple prefixes, or different naming convention), just implement strategy yourself (and configure with Provider that constructs instances).

1reaction
kennethjorcommented, Mar 24, 2020

FWIW the Lombok team have implemented a @Jacksonized annotation for making Lombok models play nicely with Jackson.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson + Builder Pattern? - java - Stack Overflow
@JsonPOJOBuilder(buildMethodName = "create", withPrefix = "set") public static class Builder { public Builder setCity(String value); ...
Read more >
More Jackson Annotations - Baeldung
The @JsonNaming annotation is used to choose the naming strategies for properties in serialization, overriding the default.
Read more >
@Jacksonized - Project Lombok
This is necessary so that Jackson recognizes them when using the builder. Insert @JsonPOJOBuilder(withPrefix="") on the generated builder class to override ...
Read more >
Jackson Deserialization and the Builder Pattern
It's very helpful when we have to create objects of classes that have many properties, especially when some of them are optional. It's...
Read more >
JSON serialization - Immutables
To enable this, you should use @JsonSerialize or @JsonDeserialize annotation. ... Style(builder = "new") // builder has to have constructor ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found