简体   繁体   English

如何为 Jackson 编写自定义 JSON 解串器?

[英]How to write custom JSON Deserializer for Jackson?

I need to deserialize some JSON to Java class.我需要将一些 JSON 反序列化为 Java class。 I have the following JSON:我有以下 JSON:

{
  "list": [[{
        "type": "text",
        "subType": "ss"
     },
     {
        "type": "image",
        "subType": "text"
     }
]]
}

and I have the following Java classes:我有以下 Java 类:

public abstract class BaseClass {
    public String type;
    public String subType;
}

public class Text extends BaseClass {
   ...
}

public class Image extends BaseClass {
}

and I need deserialize in this way, if type equals image and subType equals text I need to deserialize into Text class otherwise I need deserialize to Image class.我需要以这种方式反序列化,如果type等于image且子类型等于text ,我需要反序列化为Text subType ,否则我需要反序列化为Image class。

How can I do it?我该怎么做?

You don't need a custom deserializer.您不需要自定义反序列化器。 Mark your BaseClass with the following annotations, and deserialize with an ObjectMapper:使用以下注释标记您的 BaseClass,并使用 ObjectMapper 进行反序列化:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)
@JsonSubTypes({@JsonSubTypes.Type(value = Text.class, name = "text"), @JsonSubTypes.Type(value = Image.class, name = "image")
})
public abstract class BaseClass {
    public String type;
    public String subType;
}

JsonTypeInfo defines to use value of type field for type name. JsonTypeInfo定义使用type字段的值作为类型名称。 JsonSubTypes associates type names with java classes JsonSubTypes类型名称与 java 类相关联

You can implement your own deserializer like so:您可以像这样实现自己的反序列化器:

public class BaseClassDeserializer extends StdDeserializer<BaseClass> { 

    public BaseClassDeserializer(Class<?> vc) { 
        super(vc); 
    }

    @Override
    public BaseClass deserialize(JsonParser jp, DeserializationContext ctxt) 
      throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        String type = node.get("type").asText();
        String subType = node.get("subType").asText();

        if("image".equals(type) && "text".equals(subType)){
            /* create Text class
            return new Text */
        } else {
            /* create Image class
            return new Image(args...) */
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 杰克逊-如何编写自定义解串器 - Jackson - how to write custom deserializer 如何根据json中的属性编写杰克逊解串器 - how to write jackson deserializer based on property in json 如何编写自定义的Jackson解串器,将包含原始json的json对象反序列化为单个对象? - How do I write a custom Jackson deserializer to deserialize a json object that contains raw json into a single object? 为分层JSON映射中的特定属性编写Jackson定制反序列化器 - Write Jackson Custom Deserializer for a particular attribute in an hierarchical JSON map 如何编写一个Java自定义杰克逊解串器来解析可能为空字符串或字符串数​​组的json字段? - How to write a java custom jackson deserializer to parse json field that could be an empty string or a string array? RestTemplate自定义Jackson JSON反序列化器 - RestTemplate custom Jackson JSON deserializer 如何在 Jackson 自定义 JSON 反序列化器中使用常规 JSON 解释 - How to use regular JSON interpretation inside of a Jackson custom JSON deserializer 使用Jackson 2 Java到JSON反序列化器的自定义注释 - Custom annotation with Jackson 2 Java to JSON deserializer Jackson中的自定义Json Deserializer仅适用于Hashmap - Custom Json Deserializer in Jackson for Hashmap only Jackson - 仅针对特定JSON使用自定义反序列化程序 - Jackson - Use custom deserializer only for specific JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM