8

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

3 Answers 3

13

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.

5
  • thanks a lot that makes sense, I guess that my only option is to serialize all those other attributes needed (around 3 more complex objects :( ) for the target class constructor and use them in the MixIn class. Am I right?
    – maverick
    Commented Sep 16, 2011 at 19:38
  • Thanks again, I finally ended excluding the field while deserialization since it's not needed any longer.
    – maverick
    Commented Sep 16, 2011 at 20:47
  • having this, or something similar, would be incredibly helpful for easy deserialization of classes that cannot be modified. There are many cases were general deserialization is just too difficult.
    – loesak
    Commented Aug 22, 2016 at 16:09
  • @loesak for specific case of static factory methods this could be useful indeed. You may want to file an RFE at github issue tracker and suggest this improvement.
    – StaxMan
    Commented Aug 23, 2016 at 5:28
  • @StaxMan done. github.com/FasterXML/jackson-future-ideas/issues/10
    – loesak
    Commented Sep 29, 2016 at 21:16
0

Try using @JsonIgnoreProperties({"isMilestoneView", "milestoneId"}) Class level annotation

0

You could use a mixin with a custom deserializer

@JsonDeserialize(using = MixIn.PointDeserializer.class)
public class MixIn {
  static class PointDeserializer extends JsonDeserializer<Point> {
    @Override
    public Point deserialize(@Nullable JsonParser p, @Nullable DeserializationContext ctxt)
      throws IOException, JsonProcessingException {
      if (p == null) {
        return null;
      }
      TreeNode t = p.readValueAsTree();
      x = t.get("x");
      y = t.get("y");
      return createPoint(x,y);
    }
  }

  private static final GeometryFactory geometryFactory = GeometryFactoryFactory.getGeometryFactory();
  public static Point createPoint(@JsonProperty("x")double x, @JsonProperty("y")double y){
    return geometryFactory.createPoint(new Coordinate(x, y));
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.