【问题标题】:Using Jackson annotations with inherited class将 Jackson 注释与继承的类一起使用
【发布时间】:2014-08-17 19:35:07
【问题描述】:

我正在开发一个 Android 应用,我在其中使用 Jackson Annotation API 反序列化 JSON。

在我尝试包含 AndroidActive ORM 之前,它运行得非常好,这需要您的 POJO 从 Model 类 (https://github.com/pardom/ActiveAndroid/blob/master/src/com/activeandroid/Model.java) 继承。

我的 JSON 在 asyncTask 中被反序列化:

Reader reader = new InputStreamReader(url.openStream());
try {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    rootJsonObj = mapper.readValue(reader, MyPojo.class);
} catch (JsonGenerationException e) {
    e.printStackTrace();
}

快速浏览一下我的 pogo:

@Table(name = "RootRecipes") //AndroidActive annotation
public class RootRecipes extends Model { 

    @JsonProperty("deleted") //Jackson annotation
    @Column(name = "deleted") //AndroidActive annotation
    public ArrayList<Number> deleted; 

    @JsonProperty("meta")
    @Column(name = "meta")
    public Meta meta; 

    @JsonProperty("objects")
    @Column(name = "objects")
    public ArrayList<Objects> objects;

json 非常大,超过 2mo,但结构如下:

{"deleted": [107981, 107982, 107995, 107999, 108012, 108014], 
"meta": {"is_anonymous": true, "latest": 1405555349, "limit": 1000, "next": null, "offset": 0, "page": 1, "pages": 1, "previous": null, "total_count": 20}, 
"objects": [<more objects>]}

给我的错误是:

com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class com.example.app.json.MyPojo] value failed: null

一旦我从 Model 中删除了继承,解析就会正常工作。 我无法弄清楚这个错误的原因。

谢谢。

【问题讨论】:

  • 您能告诉我们您要反序列化的JSON 吗?
  • 我已经添加了它的结构,它非常大。然而,只要 MyPojo 不扩展 Model,Jackson 的解析就可以很好地工作。
  • 好的,谢谢。我看到这个模型类包含属于 Android 世界的元素。我认为这是Jackson 无法反序列化和实例化此对象的原因。您应该将 Android 的属性与真实数据分离。
  • 感谢您的关注。我到处看了看,我可能对杰克逊还不够熟悉,无法解耦这些属性。您对如何进行有任何猜测吗?

标签: java android orm annotations jackson


【解决方案1】:

在我看来,您应该创建新的POJO 类来使用与Android 世界分离的JSON(我的意思是,它们不能有来自Android 包的属性、父级)。它可能看起来像这样:

class JsonRootRecipes {

    @JsonProperty("deleted")
    public List<Number> deleted;

    @JsonProperty("meta")
    public JsonMeta meta;

    @JsonProperty("objects")
    public List<Object> objects;

    // getters, setters, toString
}

class JsonMeta {

    @JsonProperty("is_anonymous")
    private boolean anonymous;

    // getters, setters, toString
}

// another POJOs decoupled from Android classes.

现在,您必须创建一个服务类,该服务类将能够解析JSON 并将JsonPojo 类转换为您的Android POJO 类。伪代码:

class JsonService {

    public RootRecipes parseJson(json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        JsonRootRecipes jsonRootRecipes = mapper.readValue(json, JsonRootRecipes.class);

        RootRecipes rootRecipes = null; //convert jsonRootRecipes to RootRecipes

        return rootRecipes;
    }
}

【讨论】:

  • 不幸的是,我最担心的是,不得不将我所有与 json 相关的 POJO 加倍:'( 感谢您的时间和兴趣。如果我碰巧找到了更好的方法,我会添加它。
猜你喜欢
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2013-09-28
相关资源
最近更新 更多