I am trying to de/seralize framework objects (no source code access) into JSON using jackson 2.
class Item {
public Item(Long id) {}
}
I found this Add annotation to a parameter on a new method created with Javassist but this solution is based on JavaAssist and does not fully apply :(
The underlying issue is the lack of DefaultConstructors which can be solved using the @JsonCreator annotation together with a matching @JsonProperty annotation for the parameter.
@JsonCreator
class Item {
public Item(@JsonProperty("id") Long id) {}
}
I managed to achieve this for one of the many item subclasses using a mixin class.
public abstract class ItemChildMixin {
@JsonCreator
public ItemChildMixin(@JsonProperty("objId") final Long objId) {
}
}
However, writing mixin classes for all the relevant objects with almost the same content seems the wrong approach, so I started looking at aspects.
Adding the Annotation to the classes in the Item's hierarchy was easy:
aspect DeclareJsonCreatorAspect {
declare @constructor: Item+.new(Long): @JsonCreator;
}
However, I cannot seem to find a way to add an annotation to the constructor parameters using Aspects! Aspectj in Action as well as google did not provide an answer yet. Is this at all possible?