【问题标题】:Error serializing and deserializing json using polymorphism in java在java中使用多态对json进行序列化和反序列化时出错
【发布时间】:2021-08-27 10:23:26
【问题描述】:

我正在尝试使用多态管理不同的价值结构。下一个示例包含 2 个应该反序列化为同一个父对象的 json

 {
   "maximum": {
     "type": "type1",
     "value": {
       "USD": 1000000,
       "GBP": 500000
     }
   }
 }

 {
   "maximum": {
     "type": "type2",
     "linkedTo": "linked to item",
     "value": 2
   }
 }

我已经定义了下一个类来管理它:

@JsonInclude (Include.NON_EMPTY)
@JsonDeserialize (builder = Parent.Builder.class)
public class Parent {
  private ParentType maximum;

  private Parent(Builder builder) {
    this.maximum = builder.maximum;
  }

  public static Builder builder() { return new Builder(); }

  public ParentType getMaximum() {
    return maximum;
  }

  public static class Builder {
    private ParentType maximum;

    public Builder withMaximum(final ParentType maximum) {
      this.maximum = maximum;
      return this;
    }

    public Parent build() {
      return new Parent(this);
    }
  }
}

@JsonInclude (Include.NON_EMPTY)
@JsonDeserialize (builder = ParentType.Builder.class)
public class ParentType {

  private String type;
  private String linkedTo;
  private ParentTypeValue value;

  private ParentType(Builder builder) {
    this.type = builder.type;
    this.linkedTo = builder.linkedTo;
    this.value = builder.value;
  }

  public static Builder builder() { return new Builder(); }

  public String getType() {
    return type;
  }

  public String getLinkedTo() {
    return linkedTo;
  }

  public ParentTypeValue getValue() {
    return value;
  }

  public static class Builder {
    private String type;
    private String linkedTo;
    private ParentTypeValue value;

    public Builder withType(final String type) {
      this.type = type;
      return this;
    }

    public Builder withLinkedTo(final String linkedTo) {
      this.linkedTo = linkedTo;
      return this;
    }

    public Builder withValue(final ParentTypeValue value) {
      this.value = value;
      return this;
    }

    public ParentType build() {
      return new ParentType(this);
    }
  }
}

最后我用下一个结构尝试了 Value 的多态性:

@JsonSubTypes ({
  @JsonSubTypes.Type(value = CurrencyMapValue.class, name = "currencymapType"),
  @JsonSubTypes.Type(value = StringValue.class, name = "integerType"),
})
public abstract class ParentTypeValue {

}

@JsonTypeName ("integerType")
public class IntegerValue extends ParentTypeValue {
  private int value;

  private IntegerValue(Builder builder) {
    this.value = builder.value;
  }

  public static Builder builder() { return new Builder(); }

  public int getValue() {
    return value;
  }

  public static class Builder {
    private int value;

    public Builder withValue(final int value) {
      this.value = value;
      return this;
    }

    public IntegerValue build() {
      return new IntegerValue(this);
    }
  }
}

@JsonTypeName ("currencymapType")
public class CurrencyMapValue extends LimitTypeValue {
  private Map<Currency, Double> value;

  private CurrencyMapValue(Builder builder) {
    this.value = builder.value;
  }

  public static Builder builder() { return new Builder(); }

  public Map<Currency, Double> getValue() {
    return value;
  }

  public static class Builder {
    private Map<Currency, Double> value;

    public Builder withValue(final Map<Currency, Double> value) {
      this.value = value;
      return this;
    }

    public CurrencyMapValue build() {
      return new CurrencyMapValue(this);
    }
  }
}

我猜多态结构是错误的,因为我无法反序列化并且序列化是在想要的值中添加一个额外的值对象。我的意思是这样的:

  "maximum": {
    "type": "amount",
    "linkedTo": "linkedTo",
    "value": {
      **"value": {**
        "USD": 5000000,
        "GBP": 200000
      }
    }
  }

有人知道这有什么问题吗?非常感谢您的帮助!

【问题讨论】:

    标签: java jackson polymorphism json-deserialization json-serialization


    【解决方案1】:

    试试这个..

    请注意,@JsonSubTypes@JsonTypeInfo 注释被添加到 Maximum 类中。

    如果您只是反序列化,则不需要在 CurrencyMapValueIntegerValue 类中使用 @JsonTypeName 注释。但是我已经添加了它们。

    public abstract class ParentTypeValue {}
    
    @Getter
    @Builder
    @JsonDeserialize(builder = CurrencyMapValue.CurrencyMapValueBuilder.class)
    @JsonTypeName("type1")
    public class CurrencyMapValue extends ParentTypeValue {
        private Map<String, Double> value;
    
        @JsonPOJOBuilder(withPrefix="")
        public static class CurrencyMapValueBuilder {}
    }
    
    @Getter
    @Builder
    @JsonDeserialize(builder = IntegerValue.IntegerValueBuilder.class)
    @JsonTypeName("type2")
    public class IntegerValue extends ParentTypeValue {
        private int value;
        private String linkedTo;
    
        @JsonPOJOBuilder(withPrefix="")
        public static class IntegerValueBuilder {}
    }
    
    @Getter
    @Setter
    public class Maximum {
        @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
        @JsonSubTypes({
                @JsonSubTypes.Type(value = CurrencyMapValue.class, name = "type1"),
                @JsonSubTypes.Type(value = IntegerValue.class, name = "type2"),
        })
        private ParentTypeValue maximum;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2015-01-11
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      相关资源
      最近更新 更多