Jackson Adding a Custom Serializer for all fields of Type X

Viewed 900

I have a class like this:

public class Foo {
    private Integer id;
    private FooB fooA;
    private boolean isB;
    private boolean isC;
    private int age;
    private LocalDate date1;
    private LocalDate date2;
    private LocalDate date3;
    private LocalDate date4;
}

I want to serialize everything in this class natively except for the LocalDate fields. I have a custom serializer for that like so:

public class LocalDateSerializer extends JsonSerializer<LocalDate> {
    @Override
    public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        //serialize
    }
}

Is there a way to annotate the class Foo to say only use the serializer for objects of type LocalDate or do I have to go annotate each individual field like so:

public class Foo {
    private Integer id;
    private FooB fooA;
    private boolean isB;
    private boolean isC;
    private int age;
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate date1;
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate date2;
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate date3;
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate date4;
}

My question is, can I consolidate that into a single class level annotation?

1 Answers
Related