0

I have a couple of json properties which should be handled by multiple aanotations

From design's best practices, should I have multiple repeating annotations (from a standard library) or should I create a customized annotation to handle them together?

for example which version is beeter:

public class A{
    private static final String DATE_PATTERN = "yyyy-MM-dd";

    @JsonProperty
    private String name;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_PATTERN)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate date1;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_PATTERN)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate date2;



    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_PATTERN)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate rdate3;
}


public class B{


    @JsonProperty
    private String name;

    @customeDateFormatter
    private LocalDate date1;

    @customeDateFormatter
    private LocalDate date2;

    @customeDateFormatter
    private LocalDate rdate3;
}

where @customeDateFormatter defined in another class

Mahyar
  • 1,011
  • 2
  • 17
  • 37

2 Answers2

0

Repeating yourself is considered bad practice for many reasons, so going for the version with a custom annotation is in this example case obviously the better choice. However this really only makes sense, if the configuration is always the same as in the example. If the custom annotation could only be used once or twice, because other fields need slight variations, then this might not improve much at all.

pschichtel
  • 759
  • 7
  • 18
0

Why annotate formats and serializers at all?

Jackson will do it for you automatically, if you just register the JSR310 module.

Proof

public class A {
    @JsonProperty private String name;
    @JsonProperty private LocalDate date1;
    @JsonProperty private LocalDate date2;
    @JsonProperty private LocalDate rdate3;

    public A() {
    }

    public A(String name, LocalDate date1, LocalDate date2, LocalDate rdate3) {
        this.name = name;
        this.date1 = date1;
        this.date2 = date2;
        this.rdate3 = rdate3;
    }

    @Override
    public String toString() {
        return "A [name=" + this.name + ", date1=" + this.date1 +
               ", date2=" + this.date2 + ", rdate3=" + this.rdate3 + "]";
    }

}
ObjectMapper mapper = new ObjectMapper()
        .registerModule(new JavaTimeModule()) // or: .findAndRegisterModules()
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        ;

A a = new A("Foo", LocalDate.of(2000, 1, 1), LocalDate.now(), LocalDate.of(2999, 12, 31));
System.out.println(a);

String json = mapper.writeValueAsString(a);
System.out.println(json);

A a2 = mapper.readValue(json, A.class);
System.out.println(a2);

Output

A [name=Foo, date1=2000-01-01, date2=2018-05-01, rdate3=2999-12-31]
{"name":"Foo","date1":"2000-01-01","date2":"2018-05-01","rdate3":"2999-12-31"}
A [name=Foo, date1=2000-01-01, date2=2018-05-01, rdate3=2999-12-31]
Andreas
  • 154,647
  • 11
  • 152
  • 247