【发布时间】: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