Let's suppose there is a class hierarchy in some third-party library. One of those classes has @JsonValue
annotation. It results in serialization/deserialization of only one field. Is it possible to ignore @JsonValue
annotation without subclassing and get all object fields serialized?
You can define a MixIn and override the @JsonValue
via property or getter
- depending on where the annotation has been set in the third-party library.
Assuming we have a class Pojo
with a @JsonValue
annotated member prop
:
class Pojo {
@JsonValue
private String prop;
private String prop2;
// getters and setters
}
We then define an abstract MixIn
class like follows, overriding the particular member or method:
abstract class MixIn {
@JsonValue(false) // either annotating members
private String prop;
@JsonValue(false) // or annotating methods
abstract String getProp();
}
This can be added to the ObjectMapper
like follows:
objectMapper.addMixIn(Pojo.class, MixIn.class);
Instead of just prop
, now both prop
and prop2
are serialized.