【问题标题】:Handle different types for some fields while serializing and deserializing with Jackson在使用 Jackson 进行序列化和反序列化时处理某些字段的不同类型
【发布时间】:2018-05-18 09:15:53
【问题描述】:

我正在使用杰克逊 2。 从远程服务读取时,我获得了一个这样的 JSON 对象:

{
  "country":"1",
  "complex":{
     "link":"...",
     "value":"..."
  },
  "test":""
}

所以我为此目的创建了相关的 POJO:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "link",
    "value"
})
public class Complex {

    @JsonProperty("link")
    private String link;

    @JsonProperty("value")
    private String value;

    @JsonProperty("link")
    public String getLink() {
        return link;
    }

    @JsonProperty("link")
    public void setLink(String link) {
        this.link = link;
    }

    @JsonProperty("value")
    public String getValue() {
        return value;
    }

    @JsonProperty("value")
    public void setValue(String value) {
        this.value = value;
    }
}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "country",
    "complex",
    "test"
})
public class Resource {

    @JsonProperty("country")
    private String country;

    @JsonProperty("complex")
    private Complex complex;

    @JsonProperty("test")
    private String test;

    @JsonProperty("country")
    public String getCountry() {
        return country;
    }

    @JsonProperty("country")
    public void setCountry(String country) {
        this.country = country;
    }

    @JsonProperty("complex")
    public Complex getComplex() {
        return complex;
    }

    @JsonProperty("complex")
    public void setComplex(Complex complex) {
        this.complex = complex;
    }

    @JsonProperty("test")
    public String getTest() {
        return test;
    }

    @JsonProperty("test")
    public void setTest(String test) {
        this.test = test;
    }

}

这样我就可以做到:

Resource res = MAPPER.readValue(response.readEntity(String.class), Resource.class);

我的问题是,在执行POST请求时,我需要发送一个不同的对象,即:

{
  "country":"1",
  "complex": "value",
  "test":""
}

所以所有"complex" 对象必须是简单的字符串。

知道如何处理这种情况吗?

我尝试创建一个JsonDeserializer 类:

public class ComplexValueDeserializer extends JsonDeserializer<Object> {
    @Override
    public Object deserialize(final JsonParser parser, final DeserializationContext deserializationContext)
            throws IOException, JsonProcessingException {
        final JsonToken jsonToken = parser.getCurrentToken();

        if (jsonToken == null
                || (jsonToken == JsonToken.START_OBJECT && parser.getValueAsString().equals("{}"))) {
            return "";
        }
        return null;
    }
}

并将其添加到:

@JsonProperty("complex")
@JsonDeserialize(using = ComplexValueDeserializer.class)
private Complex complex;

但我收到 java.lang.IllegalArgumentException: argument type mismatch 错误。

谢谢!

【问题讨论】:

标签: java jackson pojo


【解决方案1】:

在您的复杂对象类型上,您可以定义一个 toString() 方法,然后使用 @JsonValue 对其进行注释。这将向 Jackson 指示返回的结果将是为该对象序列化的值。您可以在那里实现您需要的任何逻辑,以您想要的方式表示 Complex 类型。例如:

@JsonValue
public String toString() {
   return value;
}

【讨论】:

    【解决方案2】:

    为什么需要 “复杂的”:{ “关联”:”...”, “价值”:”...” }, 对于 api 文档?

    【讨论】:

    • 因为我需要在远程读取对象时访问linkvalue 字段。例如。我需要做complex.getLink()
    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多