Search code examples
javajacksonjfreechart

How to ignore certain field of a third party class when deserializing with Jackson?


I've got a class which contains field from JFreeChart, like XYPolygonAnnotation which contains itself Paint and Stroke.

I can serialize it easily using an ObjectMapper. But when I try to deserialize it I've got some issues because Paint and Stroke are interfaces so it's impossible to have a constructor for them. I tried MixIn to deserialize them but it's not enough.

      ObjectMapper mapper = new ObjectMapper();
      SimpleModule test = new SimpleModule();
      test.addAbstractTypeMapping(Paint.class, java.awt.Color.class);
      mapper.registerModule(test);
      mapper.registerModule(new Jdk8Module());
      mapper.addMixIn(XYPolygonAnnotation.class, MixInXYPolygonAnnotation.class);

And my MixIn is :

public abstract class MixInXYPolygonAnnotation {

  public MixInXYPolygonAnnotation(
      @JsonProperty("polygon") double[] polygon,
      @JsonProperty("stroke") Stroke stroke,
      @JsonProperty("outlinePaint") Paint outlinePaint,
      @JsonProperty("fillPaint") Paint fillPaint) {}
}

But then I've got this error message.

Unrecognized field "rgb" (class java.awt.Color), not marked as ignorable (4 known properties: "red", "blue", "green", "alpha"])

And here is my JSON the part of my Json which is failing.

               "polygon":{
                  "notify":true,
                  "toolTipText":null,
                  "url":null,
                  "outlinePaint":{
                     "red":242,
                     "green":114,
                     "blue":0,
                     "alpha":255,
                     "rgb":-888320,
                     "transparency":1,
                     "colorSpace":{
                        "type":5,
                        "numComponents":3,
                        "profile":{
                           "mediaWhitePoint":[
                              0.9504547,
                              1.0,
                              1.0890503
                           ],
                           "matrix":[
                              [

What should I do to be able to deserialize interfaces from 3rd party libraries ? Should I ignore some fields ? If yes how am I supposed to do this, I think I'm missing something.

EDIT :

So I added this :

@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class MixInPaint {
  public MixInPaint(
      @JsonProperty("red") int red,
      @JsonProperty("green") int green,
      @JsonProperty("blue") int blue,
      @JsonProperty("alpha") int alpha) {}
}

And my mapper looks like this :

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule test = new SimpleModule();
    test.addAbstractTypeMapping(Paint.class, Color.class);
    mapper.registerModule(new Jdk8Module());
    mapper.addMixIn(XYPolygonAnnotation.class, MixInXYPolygonAnnotation.class);
    mapper.addMixIn(Paint.class, MixInPaint.class);
    mapper.registerModule(test);
    mapper.registerModule(new Jdk8Module());
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

But now I end up with

Cannot construct instance of org.jfree.chart.annotations.XYPolygonAnnotation, problem: Null 'polygon' argument.

What am I missing here ?


Solution

  • Well I manage to get a solution to make this work.

    Instead of loading directly a XYPolygonAnnotation I loaded it's attributes and recreated the object with this constructor :

    XYPolygonAnnotation(double[] polygon, Stroke stroke, Paint outlinePaint, Paint fillPaint)
    

    So I don't need the MixIn. And for the other fields from third party libraries I created my own version of them. I had a TimeSeries and created SerializedTimeSeries to have my own constructor.