I want to add a wrapper which named is determined at runtime, because it depends of the class name (I could use @JsonRootName but I don't want to because I would have to use it on every sub class, which is not efficient).
I suppose I should use @JsonSerialize
to override the default serializer, but I want that just to create the wrapper; I don't want to serialize the object fields myself (also I am in an abstract class, so I don't even know the fields of the sub class!). I don't care about them, I just care about the wrapper! So I would like the default serializer to handle those fields for me, or something like that.
@JsonSerialize(using = CustomSerializer.class)
public abstract class Request {
public static class CustomSerializer extends JsonSerializer<Request > {
@Override
public void serialize(Request request, JsonGenerator jgen, SerializerProvider provider) throws IOException {
// Doing my stuff to determine the wrapper name based on request.class.getSimpleName()
// Then what should I wright to serialize the fields?
// Basically I just want a function to generate the same json that the default serializer would generate!
// I tried the following, but obviously it gives a com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion
jgen.writeObject(value);
// Same error for the function below
provider.defaultSerializeValue(value, jgen);
}
}