Do I need to add mixinAnnotations for all subclasses ?

468 views
Skip to first unread message

Jan-Olav Eide

unread,
Nov 24, 2014, 7:51:36 AM11/24/14
to jackso...@googlegroups.com
Hello. I have to (de)serialize objects in a library I cannot touch, I therefore need to use mixins.

All these objects have a common superclass S, which performs some magic on ots constructor arguments. I have a large number of subclasses of S.
public S (String arg1,String arg2,String arg3) {
 // Magic here
}

public class B extends S  {

public B(String arg1, String arg2,String arg3) {
   super(arg1,arg2,arg3)
}

I then put this in a mixin, toghether with typeInfo annotation

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
abstract class SMixIn{

    @JsonCreator
    public SMixIn(@JsonProperty("arg1") String arg1, @JsonProperty("arg2") String  arg2, @JsonProperty("arg3") String arg3) {
    }
....

and register it :

mapper.addMixInAnnotation(S.class, SMixIn.class);
If I now create a B and serialize/deserialize , the mixin annotations do not seem to set for it, only for the superclass S, and an appropriate constructor is not found.

Is there any way around this apart from setting the Mixin annotation on each concrete subclass ?

 mapper.addMixInAnnotation(B.class, SMixIn.class);
 mapper.addMixInAnnotation(B1.class, SMixIn.class);
 mapper.addMixInAnnotation(B2.class, SMixIn.class);


which seems clumsy since there are so many subclasses.




Tatu Saloranta

unread,
Nov 24, 2014, 1:17:38 PM11/24/14
to jackso...@googlegroups.com
Mix-in works partially here, since @JsonTypeInfo is inherited.
Method mix-ins also inherit, that is, match methods at any level.

But creator annotations are different, and only bind to specific target type, so @JsonCreator annotation is not used for sub-classes.

Originally this seemed sensible, as constructors have somewhat special behavior and it didn't feel right for them to be inheritable.
But perhaps this should be changed, since this seems like a valid use case. And there doesn't seem to be that much downside -- mix-in only matches members with matching signature, so the worst that could happen is that sub-class constructor would be missed, similar to how regular methods need to match.

So, could you go ahead and file an RFE for "make mix-ins for constructors match sub-class constructors too" or such?

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages