【问题标题】:Jackson use custom JSON deserializer with defaultJackson 默认使用自定义 JSON 反序列化器
【发布时间】:2014-09-24 10:48:31
【问题描述】:

以下所有代码均为简化版。 我有 JSON 结构:

{
    "content" : {
        "elements" : [ {
            "type" : "simple"
            },
            {
            "type" : "complex", 
            "content" : {
                "elements" : [ {
                    "type" : "simple"
                },
                {
                    "type" : "simple"
                },
                {
                    "type" : "complex",
                    "content" : {
                      ---- /// ----
                    }
                } ] 
            } 
        } ] 
    }
}

我使用 Jackson lib 进行反序列化,并且我正在尝试使用默认反序列化器实现一种“混合”自定义。 我希望使用自定义 ElementDeserializer 创建 Element 对象,但对于里面的 Content 字段使用默认值。不幸的是这样的事情:

@JsonDeserialize(using = StdDeserializer.class)
@JsonProperty
Content content;  

没用=(

这是我现在的代码:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Content {

    @JsonProperty("elements")
    ArrayList<Element> mElements;

}

@JsonDeserialize(using = ElementDeserializer.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Element<T extends ElementType> {

    @JsonProperty
    Content content;

    T mField;

    public Element(T field) {
        mField = field;
    }

}

public class ElementDeserializer extends JsonDeserializer<Element> {

    @Override
    public Element deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        Element element = null;
        JsonNode node = jp.getCodec().readTree(jp);
        if ("simple".equals(node.get("type").textValue())) {
            element = new Element(new SimpleField());
        } else if ("complex".equals(node.get("type").textValue())) {
            element = new Element(new ComplexField());
        }
        return element;
    }
}

我将不胜感激!

【问题讨论】:

    标签: java json jackson deserialization json-deserialization


    【解决方案1】:

    不确定您是否必须使用自定义反序列化器(原因未在您的帖子中说明)。如果不是,那么你可以不用它,使用默认的反序列化器。

    方法如下:

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class TopObject {
        @JsonProperty
        public Content content;
    
        public TopObject() {
        }
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Content {
    
        @JsonProperty
        public Element elements [];
    
        public Content() {
        }
    }
    
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes({ 
        @Type(value = SimpleElement.class, name = "simple"),
        @Type(value = ComplexElement.class, name = "complex")
    })
    public class Element {     
        public Element() {
        }
    }
    
    public class SimpleElement extends Element {
        public SimpleElement() {
        }
    }
    
    public class ComplexElement extends Element {    
        @JsonProperty
        public Content content;
    
        public ComplexElement() {
        }
    }
    

    然后将json数据反序列化为TopObject.class

    【讨论】:

    • 非常感谢您的回答——非常有趣!我最近不知道 JsonTypeInfo 和 JsonSubTypes。我将尝试在我的情况下使用此解决方案。
    猜你喜欢
    • 2018-07-15
    • 2011-04-12
    • 2013-10-10
    • 2014-08-02
    • 2016-01-29
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    相关资源
    最近更新 更多