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.

`@JsonNaming` on value class not applied to use via Builder

See original GitHub issue

Given an input json string like "{\"Id\":1,\"Title\":\"title\",\"ShortDescription\":\"desc\"}", I’d like to keep java naming conventions on my class, and use PropertyNamingStrategy.UpperCamelCaseStrategy for deserialization:

    @lombok.Data
    @lombok.Builder
    @lombok.NoArgsConstructor
    @lombok.AllArgsConstructor(access = AccessLevel.PACKAGE)
    @JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
    public class Foo {
        private Long   id;
        private String title;
        private String shortDescription;
    }

While this works as intended, if I wanted to make the class (sort of) immutable I’d have to remove the no args constructor, and use the builder to deserialize:

    @lombok.Value
    @lombok.Builder
    @lombok.AllArgsConstructor(access = AccessLevel.PACKAGE)
    @JsonDeserialize(builder = Foo.FooBuilder.class)
    @JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
    public class Foo {
        private Long   id;
        private String title;
        private String shortDescription;
    }

but this results in an empty bean. The builder setters are never invoked, Jackson cannot match them to the input properties:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String json = "{\"Id\":1,\"Title\":\"title\",\"ShortDescription\":\"desc\"}";
mapper.readValue(json, Foo.class);
//Foo(id=null, title=null, shortDescription=null)

Currently on Jackson 2.10.3.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, May 6, 2020

@JsonNaming will need to be added to Builder class, not value class – this is basically the rule for all builder-based functionality, mostly due to implementation details. So that would solve the issue in your case, if Lombok can apply it there (or is configurable).

But I think it is reasonable to suggest that at least some annotations from value class should be sort of added as defaults to be used with builder. So I think I can keep this as a placeholder for such request(s) – I may create separate issue for more general functionality, but at least for now this works.

0reactions
cowtowncodercommented, May 24, 2020

@janrieke Wow. That looks really really nice. And even if Jackson was to do this eventually on its own, it is good to have ability to customize handling (and obviously have this available now/soon, instead of “maybe in future”).

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonNaming not working with lombok builder - Stack Overflow
Above one is working fine and able to deserialize. But if I use lombok builder for deserialization, it's failing. Below is class with...
Read more >
More Jackson Annotations - Baeldung
The @JsonNaming annotation is used to choose the naming strategies for properties in serialization, overriding the default. Using the value ...
Read more >
Jackson Deserialization and the Builder Pattern
It's common that when we use builders the constructors of our classes are private and instances can only be created through the builder....
Read more >
Using Jackson annotations with Jackson-jr | by @cowtowncoder
public class ValueHolder { // not needed for setting or getting, name does not matter private int v; @JsonProperty("value")
Read more >
Lombok - the wrong way - SoftwareMill
As you might know, Lombok is used to simplify Java development, ... You can also use a builder to construct instances of the...
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