如何在使用 Jackson 反序列化时忽略第三方 class 的某些字段?

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

我有一个 class,其中包含来自 JFreeChart 的字段,例如 XYPolygonAnnotation,它本身包含 PaintStroke.

我可以使用 ObjectMapper 轻松地将其序列化。但是当我尝试反序列化它时,我遇到了一些问题,因为 PaintStroke 是接口,所以不可能有它们的构造函数。我尝试使用 MixIn 反序列化它们,但这还不够。

      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);

我的 MixIn 是:

public abstract class MixInXYPolygonAnnotation {

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

但是我收到了这个错误信息。

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

这是我的 JSON 我的 Json 失败的部分。

               "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":[
                              [

我应该怎么做才能反序列化来自第 3 方库的接口?我应该忽略某些字段吗?如果是,我应该怎么做,我想我错过了一些东西。

编辑:

所以我添加了这个:

@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) {}
}

我的映射器看起来像这样:

    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);

但现在我得到了

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

我在这里错过了什么?

好吧,我设法找到了一个解决方案来完成这项工作。

我没有直接加载 XYPolygonAnnotation,而是加载了它的属性并使用此构造函数重新创建了对象:

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

所以我不需要 MixIn。对于第三方库中的其他字段,我创建了自己的版本。我有一个 TimeSeries 并创建了 SerializedTimeSeries 来拥有我自己的构造函数。