Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is wrong with my @JsonCreator and MixIn annotation?

Tags:

java

jackson

I'm currently using jackson 1.7 attempting to deserialize an object from a third party library.

So I set up my ObjectMapper to use my mixIn class like this:

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.getDeserializationConfig().addMixInAnnotations(com.vividsolutions.jts.geom.Point.class, MixIn.class);

And my MixIn class annotated with @JsonCreator and with the logic to instantiate the Point object there

public class MixIn {
private static final GeometryFactory geometryFactory = GeometryFactoryFactory.getGeometryFactory();

@JsonCreator
public static Point createPoint(@JsonProperty("x")double x, @JsonProperty("y")double y) {
    return geometryFactory.createPoint(new Coordinate(x, y));
}}

But I'm getting the exception

No suitable constructor found for type [simple type, class com.vividsolutions.jts.geom.Point]: can not instantiate from JSON object (need to add/enable type information?)

Debugging shows that my MixIn class is never called, I thought that it needed to be concrete class but had the same result.

What am I doing wrong? What is wrong with my configuration?

Thanks

like image 796
maverick Avatar asked Sep 15 '11 23:09

maverick


1 Answers

The problem is in assumption that mix-ins would be used for anything other than adding annotations. So in your case, annotation for 'createPoint()' would be added, but unless target class has matching factory method (to add annotations to), this will not have any effect. Specifically, then, mix-ins can not be used to inject static factory methods; they can only be used to associate annotations with existing classes.

like image 71
StaxMan Avatar answered Sep 30 '22 20:09

StaxMan