如何在Jackson中反序列化嵌套包装的String? [英] How can I deserialize a nested wrapped String in Jackson?

查看:107
本文介绍了如何在Jackson中反序列化嵌套包装的String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,其中包含嵌套和包装的JSON字符串.我想使用Jackson来反序列化,但不确定如何.这是一个示例bean:

I have a JSON string that contains a nested and wrapped JSON string. I'd like to deserialize this using Jackson but I'm unsure how. Here's a sample bean:

@JsonIgnoreProperties(ignoreUnknown = true)
public class SomePerson {

    public final String ssn;
    public final String birthday;
    public final Address address;

    @JsonCreator
    public SomePerson(
            @JsonProperty("ssn") String ssn,
            @JsonProperty("birthday") String birthday,
            @JsonProperty("data") Address address,
            @JsonProperty("related") List<String> related) {
        this.ssn = ssn;
        this.birthday = birthday;
        this.address = address;
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Address {

        public final String city;
        public final String country;

        @JsonCreator
        public Address(
                @JsonProperty("city") String city,
                @JsonProperty("country") String country) {
            this.city = city;
            this.country = country;
        }
    }
}

JSON字符串类似于:

The JSON string resembles this:

{
  ssn: "001",
  birthday: "01.01.2020",
  address: "{ city: \"London\", country: \"UK\" }"
}

虽然我之前对nsted对象进行了反序列化-当对象是一个包装好的字符串时,我不知道该怎么做.

While I've deserialized nsted objects before - I'm rather lost as to how to do this when the object is a wrapped string.

推荐答案

当内部对象转义为 JSON字符串时,我们需要将其反两次"反序列化.第一次是在反序列化根 JSON Object 时运行的,第二次是我们需要手动运行的.为此,我们需要实现自定义反序列化程序,该程序实现 com.fasterxml.jackson.databind.deser.ContextualDeserializer 接口.看起来可能像这样:

When internal object is escaped JSON String we need to deserialise it "twice". First time is run when root JSON Object is deserialised and second time we need to run manually. To do that we need to implement custom deserialiser which implements com.fasterxml.jackson.databind.deser.ContextualDeserializer interface. It could look like this:

class FromStringJsonDeserializer<T> extends StdDeserializer<T> implements ContextualDeserializer {

    /**
     * Required by library to instantiate base instance
     * */
    public FromStringJsonDeserializer() {
        super(Object.class);
    }

    public FromStringJsonDeserializer(JavaType type) {
        super(type);
    }

    @Override
    public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String value = p.getValueAsString();

        return ((ObjectMapper) p.getCodec()).readValue(value, _valueType);
    }


    @Override
    public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
        return new FromStringJsonDeserializer<>(property.getType());
    }
}

我们需要使用此类来注释我们的媒体资源:

We need to annotate our property with this class:

@JsonDeserialize(using = FromStringJsonDeserializer.class)
public final Address address;

从现在开始,您应该能够在 JSON 有效负载之上反序列化到您的 POJO 模型.

Since now, you should be able to deserialise above JSON payload to your POJO model.

另请参阅:

这篇关于如何在Jackson中反序列化嵌套包装的String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆