`@JsonNaming` on value class not applied to use via Builder
See original GitHub issueGiven 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:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
@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”).