Configuring multiple serializers and deserializers to properties with a specific annotation

27 Views Asked by At

I need to be able to serialize a number of non-standard strings to Instants in several JSON objects. I also need to save these values to a data store, and would like to use the standard ISO-8601 format so that the fields will be sortable. I would like to keep as much of the configuration as possible isolated away from the main codebase, so the models for the JSON objects are in a different module, along with all the autoconfiguration for Spring Boot.

I apply the serializer/deserializer like so:

public class Model {
  @JsonSerialize(using = CustomSerializer.class)
  @JsonDeserialize(using = CustomDeserializer.class)
  @Getter
  @Setter
  Instant time;
}

The problem with this approach is that there is no way for me to remove these serializers/deserializers when I want to persist the model and use the normal Instant serializer/deserializer.

I could just create a separate ObjectMapper for the persistance, but then I would lose the rest of the auto-configuration.

I could use mix-ins, but I would need to maintain extra models for all the classes that contain the special format.

I could apply the Serializer/deserializer by applying the annotations to a separate annotation that would be applied to the property, but I would still run into the same problem I started with: hard-coded serializers and deserializers that could not be overridden.

@Retention(Retention.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@JsonSerialize(using = CustomSerializer.class)
@JsonDeserialize(using = CustomDeserializer.class)
@JacksonAnnotationsInside
public @interface @CustomInstant {}

public class Model {
  @CustomInstant
  @Getter
  @Setter
  Instant time;
}

Is there another way to apply the serializer/deserializer via an annotation, or check for the presence of an annotation and apply the serializer and deserializer via a mix-in? Like taking @JsonSerialize/@JsonDeserialize off the annotation, then apply the (de)serializers via a mix-in applied to @CustomInstant?

0

There are 0 best solutions below