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

Add a way to define property name aliases #1029

Closed
cowtowncoder opened this issue Nov 29, 2015 · 6 comments
Closed

Add a way to define property name aliases #1029

cowtowncoder opened this issue Nov 29, 2015 · 6 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

Although it is possible to use a work-around for accepting alternate property names (by adding secondary setters), it would probably be better to allow an explicit annotation support.
This could be something as easy as:

 @JsonProperty(value="name", alias={ "oldName", "anotherOldName" })

and would work so that alternative names would be also accepted, but would not be considered the primary name (would not affect serialization for example).

This feature would seem most useful for versioning purposes, to essentially allow for renaming of properties. This is how other formats like Avro handle backwards compatibility.

@cowtowncoder cowtowncoder changed the title (2.8) Add a way to define property name aliases Add a way to define property name aliases Oct 5, 2016
@cowtowncoder cowtowncoder added this to the 2.9.0 milestone Feb 27, 2017
@indiascope
Copy link

indiascope commented Aug 11, 2017

In jackson i want to use alternate field. i get response in fullname, fullName,userFullName. No any fixed key.i try jsonAlias but i always get null value.if i make any mistake please met me know.

@JsonAlias({"userFullName","fullname","fullName"})
private String userFullName;

public String getUserFullName() {
    return userFullName;
}

public void setUserFullName(String userFullName) {
    this.userFullName = userFullName;
}

@cowtowncoder
Copy link
Member Author

@indiascope Please file a new issue with full code to reproduce the problem -- while snippet above is useful it is not enough to reproduce the problem without specifying input and code to call deserialization.

@indiascope
Copy link

Thanks for reply. After update project all jackson library to 2.9.0 problem is solved.before update jackson-databind to 2.9.0 jsonAlias not working always get null data.

compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0';
compile 'com.squareup.retrofit2:converter-jackson::2.9.0';
compile 'com.fasterxml.jackson.core:jackson-annotations::2.9.0';

@cowtowncoder
Copy link
Member Author

@indiascope Yes, that would make sense as this feature was added in Jackson 2.9, so earlier versions do not support it.

@sschuberth
Copy link
Contributor

Is there also a way to configure the alias feature globally, similar to a PropertyNamingStrategy, so that deserialization accepts multiple property names (like undersorces and camel-case)? That would be great for backwards compatibility with existing JSON files when migrating from one naming style to the other.

@cowtowncoder
Copy link
Member Author

@sschuberth In future, please ask questions on mailing lists.

But no, there is no mechanism for doing that at this point. Naming strategy does one-way transformation at this point.

I think the idea has merit, though, and since we are working for Jackson 3.0, perhaps interface could be extended to allow production of aliases too? Could you please file an issue here, outlining your idea (and with a ref back to this issue if it makes sense).

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

3 participants