在Jackson中使用自定义JSON反序列化

时间:2016-09-26 11:57:32

标签: json jackson json-deserialization

我正在尝试使用Jackson库创建复杂类的对象。每个对象都有一个架构,解串器需要使用该架构来解释JSON。我的问题是如何将模式提供给反序列化器?

反序列化器扩展了类JSONDeserializer,它具有无参数构造函数和必须重写的抽象方法反序列化(解析器,上下文)。我想使用替代方法反序列化(解析器,上下文,值),其中value是部分构造的对象,其中包括模式。也就是说,deserialize方法可以调用value.schema()来访问模式。对象本身是与构建器一起构造的,备用方法使用它。

我没有找到关于如何使用对象映射器注册备用反序列化方法的文档,以确保调用它而不是被覆盖的抽象方法。

任何建议都将受到赞赏。

1 个答案:

答案 0 :(得分:1)

因此,假设您有一个名为User的类,其中有一个名为Data的属性,其日期字段为birthdate,您不希望使用标准日期反序列化器,并希望使用您的自定义。以下是如何实现的目标。

  

User.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class User {

    @JsonProperty("code")
    public Integer code;
    @JsonProperty("status")
    public String status;
    @JsonProperty("message")
    public String message;
    @JsonProperty("time")
    public String time;
    @JsonProperty("data")
    public Data data;

}
  

Data.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Data {

    @JsonProperty("id_hash")
    public Integer idHash;
    @JsonProperty("user_name")
    public String userName;
    @JsonProperty("user_surname")
    public String userSurname;
    @JsonProperty("birthdate")
    @JsonDeserialize(using = BirthdayDeserializer.class)
    public Date birthdate;
    @JsonProperty("height")
    public Integer height;
    @JsonProperty("weight")
    public Integer weight;
    @JsonProperty("sex")
    public Integer sex;
    @JsonProperty("photo_path")
    public String photoPath;

}
  

BirthdayDeserializer.java

public class BirthdayDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = jsonparser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

}
  

Main.java来测试它。

public class Main {

    public static void main(String[] args) throws IOException {

        String json = "{\n" +
                "    \"code\": 1012,\n" +
                "    \"status\": \"sucess\",\n" +
                "    \"message\": \"Datos Del Usuario\",\n" +
                "    \"time\": \"28-10-2015 10:42:04\",\n" +
                "    \"data\": {\n" +
                "        \"id_hash\": 977417640,\n" +
                "        \"user_name\": \"Daniel\",\n" +
                "        \"user_surname\": \"Hdz Iglesias\",\n" +
                "        \"birthdate\": \"1990-02-07\",\n" +
                "        \"height\": 190,\n" +
                "        \"weight\": 80,\n" +
                "        \"sex\": 2\n" +
                "    }\n" +
                "}";
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Date.class, new BirthdayDeserializer());
        mapper.registerModule(module);
        User readValue = mapper.readValue(json, User.class);
        System.out.println(readValue);
    }
}

检查主要方法,了解我如何注册自定义Deserializer