FasterXML / jackson-future-ideas

Repository for SOLE PURPOSE of issue tracker and Wiki for NEW IDEAS. Please: NO BUG REPORTS.
18 stars 6 forks source link

@JsonProperty support for an array of property names #23

Closed tbeech closed 6 years ago

tbeech commented 6 years ago

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 commented 6 years ago

Would @JsonAlias from 2.9 work?

https://github.com/FasterXML/jackson-annotations/issues/116

tbeech commented 6 years ago

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 commented 6 years ago

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