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.

Duplicate properties with upper-case field name and JsonProperty annotation

See original GitHub issue

Duplicate 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
cowtowncodercommented, Apr 20, 2017

It works because all accessors initially match: “getAge()”, “setAge()” and “age” infer age; and renaming of one renames them all as Age. 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.

3reactions
cowtowncodercommented, Apr 19, 2017

Not a bug but results from incompatible field/getter/setter naming – fields would imply properties Name and Age (no annotation needed), but setters would imply properties name and age. 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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