public
class
UserEventDeserializer
extends
StdDeserializer<User>
implements
ResolvableDeserializer {
private
final
JsonDeserializer<?> defaultDeserializer;
public
UserEventDeserializer(JsonDeserializer<?> defaultDeserializer) {
super(User.
class
);
this.defaultDeserializer = defaultDeserializer;
}
@Override
public
User deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
User deserializedUser = (User) defaultDeserializer.deserialize(jp, ctxt);
return
deserializedUser;
}
@Override
public
void resolve(DeserializationContext ctxt) throws JsonMappingException {
((ResolvableDeserializer) defaultDeserializer).resolve(ctxt);
}
public
static
void main(String[] args) throws Exception {
SimpleModule module =
new
SimpleModule();
module.setDeserializerModifier(
new
BeanDeserializerModifier() {
@Override
public
JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
JsonDeserializer<?> deserializer) {
if
(beanDesc.getBeanClass() == User.
class
)
return
new
UserEventDeserializer(deserializer);
return
deserializer;
}
});
ObjectMapper mapper =
new
ObjectMapper();
mapper.registerModule(module);
User user = mapper.readValue(
new
File(
"test.json"
), User.
class
);
}
}