如何将JSON反序列化为复杂的POJO<>与通用对象列表

时间:2016-07-14 12:59:01

标签: json jackson json-deserialization

我使用jackson-databind 2.8.0 我有通用数据的对象

public class JsonItem<T> implements Serializable {

    private static final long serialVersionUID = -8435937749132073097L;

    @JsonProperty(required = true)
    private boolean success;

    @JsonProperty(required = false)
    private T data;

    @JsonProperty(required = false)
    private Map<String, String> errors = new HashMap<>();

    JsonItem() {
    }

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, String> getErrors() {
        return errors;
    }

    public void setErrors(Map<String, String> errors) {
        this.errors = errors;
    }
  }

并拥有对象

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class DepositInfoDto implements Serializable {

    private static final long serialVersionUID = -4123441934244992311L;

    @NotNull
    @JsonProperty(required = true)
    private String productName;

    @NotNull
    @JsonProperty(required = true)
    private String contractName;

    @NotNull
    @JsonProperty(required = true)
    private List<ContractDto> contracts;

    @NotNull
    @JsonProperty(required = true)
    private StatusDto status;
//...getters and setters
}

我收到像JsonItem<List<DepositInfoDto>>这样的对象。

我尝试创建通用方法来实现deserealize

 public <T> List<T> getObjects(){
       ObjectMapper mapper = new ObjectMapper();
       List<T> myObjects = mapper.readValue(jsonInput, new TypeReference<JsonItem<List<T>>(){});
    return myObjects;
    }

不起作用,因为T在运行时转换为Object

  public List<DepositInfoDto> getObjects(){
       ObjectMapper mapper = new ObjectMapper();
       List<DepositInfoDto> myObjects = mapper.readValue(jsonInput, new TypeReference<JsonItem<List<DepositInfoDto >>(){});
    return myObjects;
    }

工作,但我想要通用方法,因为我有DepositInfoDto, CardinfoDto, ContractDto等。

我看到了方法

public List<T> getObjects(Class<T> clazz){
       ObjectMapper mapper = new ObjectMapper();
       List<T> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
    return myObjects;
    }

但由于我JsonItem数据为List<T>

,因此无效

我该如何解决这个问题?也许mapper.getTypeFactory()有像mapper.getTypeFactory().constructType(JsonItem.class, List.class,DepositInfoDto.class)

这样复杂的方法

修改

就我而言

ObjectMapper mapper = new ObjectMapper();
                    try {
                        JsonItem<T> item  = mapper.readValue(objectWrapper.get(0), mapper.getTypeFactory().constructParametricType(
                                JsonItem.class, mapper.getTypeFactory().constructCollectionType(List.class, resourceClass)));
                        return item.getData();
                    } catch (IOException e) {
                        LOG.error("Can't deserialize JSON to class: "+ resourceClass +". Error: " + e);
                        Thread.currentThread().interrupt();
                    }

1 个答案:

答案 0 :(得分:1)

您可以使用TypeFactory#constructParametricTypeJavaType创建JsonItem<T>,然后使用TypeFactory#constructCollectionTypeCollectionType创建List<JsonItem<T>>。以下是示例:

public <T> List<JsonItem<T>> getObjects(String jsonInput, Class<T> clazz) {

    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(
           List.class, mapper.getTypeFactory().constructParametricType(JsonItem.class, clazz)));
}