Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate property produced #729

Closed
simopete opened this issue Mar 21, 2015 · 4 comments
Closed

Duplicate property produced #729

simopete opened this issue Mar 21, 2015 · 4 comments

Comments

@simopete
Copy link

Hi there,
I made a simple class with a @JsonProperty annotated private field named eSenseValues. The field has getter method named getESenseValues().
When I have serialized this class the json output had this field/property twice.
I think, it's a bug.

Here is my class and the json output:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;

public class WaveValues implements Serializable {

    @JsonProperty(value = "ESenseValues")
    private ESenseValues eSenseValues;

    public WaveValues() {
    }

    public ESenseValues getESenseValues() {
        return eSenseValues;
    }
}
{
  "WaveValues" : {
    "esenseValues" : {
      "attention" : 0,
      "meditation" : 0
    },
    "ESenseValues" : {
      "attention" : 0,
      "meditation" : 0
    }
  }
}
@cowtowncoder
Copy link
Member

Ok. This is caused by Jackson's name discovery, which by default considers getESenseValues() to mean existence of property esenseValues. This is different from Java Bean introspection which would instead infer ESenseValues. With Java Bean name handling, you would only get a single property as you expect.

Jackson 2.5 did add MapperFeature.USE_STD_BEAN_NAMING, enabling of which gets handling you want. It is disabled by default for backwards-compatibility reasons.

Another alternative (and only choice if you are using an earlier version) would be to annotate getter with @JsonProperty as well; if so, field and getter would be properly coupled.

@simopete
Copy link
Author

Thanks for your answer! I now understand the difference between Jackson and JavaBean serialization.
Before I wrote this issue I have looked at the wiki pages, but this feature isn't noted.
Can you write about it to the MapperFeature's wiki page or should I?

@cowtowncoder
Copy link
Member

@simopete I can add that to Jackson github wiki page -- thank you for pointing it out. Feel free to send PRs for updates, fixes too of course, but I can take care of this one now.

@simopete
Copy link
Author

Thank you for your work! I read the wiki again! It's good 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants