Skip to content

@JsonProperty support for an array of property names #23

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

Closed
tbeech opened this issue Apr 3, 2018 · 3 comments
Closed

@JsonProperty support for an array of property names #23

tbeech opened this issue Apr 3, 2018 · 3 comments

Comments

@tbeech
Copy link

tbeech commented Apr 3, 2018

Often times when developing against a data model that may change (whether third party API or your own application models) where backwards compatibility is needed it would be nice to use the builder pattern to support multiple properties that map to the same internal field.

@JsonDeserialize( builder = MyClass.MyClassBuilder.class )
public class MyClass {
      private final String propertyA;

      MyClass( String propertyA ) {
        this.propertyA = propertyA;
        this.propertyB = propertyB;
      }

      // The updated class and eventStatus represents the most current version of the
      // data model
      @JsonProperty( "eventStatus" )
      public getPropertyA() {
        return this.propertyA;
      }

      @JsonPojoBuilder
      public static class MyClassBuilder {
        String eventStatus = "Success";

        @JsonProperty( { "eventCode", "eventStatus" } )
        public MyClassBuilder withEventStatus( String eventStatus ) {
          this.eventStatus = eventStatus;
          return this;
        }
      }
}
@cowtowncoder
Copy link
Member

Would @JsonAlias from 2.9 work?

FasterXML/jackson-annotations#116

@tbeech
Copy link
Author

tbeech commented Apr 4, 2018

Let me RTFM the docs for that annotation, but I would guess that it would do exactly what I’m looking for. Thank you!

@cowtowncoder
Copy link
Member

@tbeech Np, hope it works out -- brief overview of 2.9 at:

https://medium.com/@cowtowncoder/jackson-2-9-features-b2a19029e9ff

and @JsonAlias is definitely something that was high on most-wanted list.
Alternative would have been to extend @JsonProperty just like you suggested.

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