@Builder with jackson JsonDeserialize

7,408 views
Skip to first unread message

Bert Cotton

unread,
Dec 13, 2014, 2:45:16 PM12/13/14
to project...@googlegroups.com
I'm working on getting the @Builder to work with an immutable class and be able to let the class be deserialized using the Jackson JsonDeserialize.  The problem I'm running into is that the @Builder uses fluent setters, not "with-methods".  Because of this the JsonDeserialize is not picking up on the fields/methods.

A comment was made in the proposed changes to the @Builder that would remove the fluent setting because no one is using the functionality.  While I can't utilize the current non-fluent functionality ("set.."), if it did the "with-methods" that would help greatly.

Alternatively, if I could add an annotation to the Builder class, I could also solve this.  By adding @JsonPOJOBuilder(withPrefix = "") to the builder I should be able to use the fluent methods for the builder and have the serialization work.

Thanks,
Bert

Sample:

@Data
@Builder(
@JsonDeserialize(builder = Book.BookBuilder.class)
// Something like this where I could add annotation to the builder
// @Builder(onBuilder == @__(@JsonPOJOBuilder(withPrefix = "")))
public final class Book
{
  public final int id;
  public final int name;
}



Roel Spilker

unread,
Dec 14, 2014, 5:19:36 PM12/14/14
to project...@googlegroups.com
You can create an empty BookBuilder class annotated with @JsonPOJOBuilder inside the book class. Lombok will use that class and add the missing fields and method.

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bert Cotton

unread,
Dec 14, 2014, 9:54:00 PM12/14/14
to project...@googlegroups.com
I was thinking about this after posting it, but hadn't had a chance to try it out.  Glad to know that this is will work.

Thanks for your very prompt reply!

Pierre Carrier

unread,
Mar 24, 2015, 6:23:25 PM3/24/15
to project...@googlegroups.com
On Sunday, December 14, 2014 at 2:19:36 PM UTC-8, Roel Spilker wrote:
> You can create an empty BookBuilder class annotated with @JsonPOJOBuilder inside the book class. Lombok will use that class and add the missing fields and method.

As Jackson is full of introspection magic, any chance you could provide a minimal example?

I'm stuck on a trivial example at https://gist.github.com/pcarrier/14d3a8e249d804cfbdee

Thanks,

-- 
Pierre Carrier

Pierre Carrier

unread,
Mar 24, 2015, 6:31:32 PM3/24/15
to project...@googlegroups.com
On Tuesday, March 24, 2015 at 3:23:25 PM UTC-7, Pierre Carrier wrote:

Unstuck myself.
withPrefix = "" was sufficient, gist updated.

Fantastic!

-- 
Pierre Carrier
Reply all
Reply to author
Forward
0 new messages