Duplicate properties with upper-case field name and JsonProperty annotation
See original GitHub issueDuplicate properties with upper-case field name and JsonProperty annotation
- Jackson version: 2.8.8
- TestCase
public class JacksonTest {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
Person person = new Person("neal", 30);
try {
objectMapper.writerWithDefaultPrettyPrinter().writeValue(System.out, person);
//new ByteArrayInputStream(objectMapper.writeValueAsBytes(person));
} catch (IOException e) {
e.printStackTrace();
}
}
private static class Person {
Person(String name, int age) {
this.Name = name;
this.Age = age;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
@JsonProperty("Name")
private String Name;
@JsonProperty("Age")
private int Age;
}
}
- Output:
{
"name" : "neal",
"age" : 30,
"Name" : "neal",
"Age" : 30
}
Expected:
{
"Name" : "neal",
"Age" : 30
}
- Suspect cause: com.fasterxml.jackson.databind.ser.BeanSerializerFactory:138
Neal
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Duplicate json property when converting java object to json ...
The simple fix for this issue is to just add annotations to either getters/setters (either is fine.) @JsonProperty("UserName") public String ...
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 >How to customize property names and values with System ...
In this article. Customize individual property names; Use camel case for all JSON property names; Use a custom JSON property naming policy ...
Read more >Definitive Guide to Jackson ObjectMapper - Serialize and ...
We've covered the @JsonProperty and @JsonAlias annotations to "bridge" mismatching field names, before converting Java Objects into JSON data.
Read more >Re: [jackson-user] Duplicate fields in json due to inconsistent ...
This only affects the specific case of single-lower-case-letter. > > There are 2 ways to resolve this: > > 1. Move `@JsonProperty` annotation...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
It works because all accessors initially match: “getAge()”, “setAge()” and “age” infer
age
; and renaming of one renames them all asAge
. Same for “name”. Introspection always starts by checking inferred name: getters and setters use bean naming convention, field names used as is. Accessors with same inferred name are grouped into one logical property candidate. After this, annotations are checked and apply to candidates, as a group.Not a bug but results from incompatible field/getter/setter naming – fields would imply properties
Name
andAge
(no annotation needed), but setters would imply propertiesname
andage
. Those will not automatically match (there is no assumption of case unification), so you end up with twice the properties you expect.Fortunately there is a simple fix: just move annotations to either setters or getters (either one is fine): this will allow otherwise separate fields and getter/setter pairs to match