I want to deserialize a data structure that has several lists containing objects of a specific subtype - i.e. several List<? extends CustomElement>. Each list is a different subclass of CustomElement. When setting these fields, I need to do some additional actions each time an element is added to the list, so I have several addElement(CustomElement) methods that handle this when the structure is being created with code. An example:
private List<CustomResourceCost> costs;
private List<CustomEffect> effects;
public void addCost(CustomResourceCost cost) {
//other actions
costs.add(cost);
}
public void addEffect(CustomEffect effect) {
//other actions
effects.add(effect);
}
(CustomResourceCost and CustomEffect both extend CustomElement)
Is there a way to make sure Jackson uses these addX methods, without each time adding a setter like the following? I have about 50 instances where this used, so would prefer not to repeat this over and over.
public void setEffects(List<CustomEffect> effects) {
effects.clear();
effects.forEach(e -> addEffect(e));
}
Note that deserializing the List<CustomElement> is not a problem, this happens correctly with default Jackson behaviour. Rather, I want to customize the way a setter method is called for this type of object (without requiring a new setter method for each instance). Is there a way to specify your own custom setter method based on the generic object type?