【发布时间】:2015-02-23 10:15:17
【问题描述】:
我有一个小的 POJO,包含一个 ArrayList (items)、一个 String (title) 和一个 Integer (id)。由于这是一个对象,我必须 a) 围绕“items”属性的 List 接口方法实现我自己的包装方法,或者 b) 将 items 公开(该列表会发生很多事情)。
编辑:为了使上述观点更清楚,我需要在反序列化后访问列表 执行添加/删除/获取/等操作 - 这意味着我需要在我的类中编写包装方法或公开列表,我不想这样做。
为了避免这种情况,我只想直接扩展 ArrayList,但我似乎无法让它与 Jackson 一起使用。给定一些像这样的 JSON:
{ "title": "my-title", "id": 15, "items": [ 1, 2, 3 ] }
我想将title 反序列化到title 字段中,对于id 也是如此,但是我想用items 填充我的类。
看起来像这样的东西:
public class myClass extends ArrayList<Integer> {
private String title;
private Integer id;
// myClass becomes populated with the elements of "items" in the JSON
}
我尝试了几种方法来实现这一点,但都失败了,即使是这样的事情:
private ArrayList<Integer> items = this; // total long shot
我想要完成的只是杰克逊无法完成的事情吗?
【问题讨论】:
标签: java json serialization jackson