Skip to content

Add a way to define property name aliases #1029

Closed

Description

@cowtowncoder

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.

Activity

  1. changed the title [-](2.8) Add a way to define property name aliases[/-] [+]Add a way to define property name aliases[/+] on Oct 5, 2016
  2. added this to the 2.9.0 milestone on Feb 27, 2017
  3. added a commit that references this issue on Feb 27, 2017
  4. indiascope commented on Aug 11, 2017

    @indiascope

    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;
    }
    
  5. cowtowncoder commented on Aug 14, 2017

    @cowtowncoder
    MemberAuthor

    @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.

  6. indiascope commented on Aug 16, 2017

    @indiascope

    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';

  7. cowtowncoder commented on Aug 16, 2017

    @cowtowncoder
    MemberAuthor

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

  8. sschuberth commented on Jun 13, 2018

    @sschuberth
    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.

  9. cowtowncoder commented on Jun 13, 2018

    @cowtowncoder
    MemberAuthor

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions