内部类的自定义杰克逊映射 [英] Custom Jackson Mapping for Inner Classes

查看:90
本文介绍了内部类的自定义杰克逊映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Jackson进行JSON视图的Spring MVC应用程序。
假设我有两个类如下。

I'm developing an Spring MVC application that uses Jackson for JSON views. Suppose that I have two classes like following.

class A {
   String prop;
   B other;
   // getters setters
}

class B {
   Integer id;
   String name;
}

我想要解决的是获取如下的JSON字符串。完全初始化A的实例

What i'm trying to resolve is to get a JSON string like following. for a fully initialized instance of A

{prop:"something", name:"otherthing"} // in here the class B 
                                      // is replaced with its propery name

insted of

insted of

{prop:"something, other:{id:5,name:"otherthing"}}

注意:上面提到的两个类是Hibernate的实体类。
我用@JsonIgnore消除了JSON输出中的一些属性,是否可以使用注释将
替换为具有其属性的子类?

NOTE: the two classes mentioned above are Entity classes for Hibernate. I have used @JsonIgnore to eliminate some properties from JSON output, Is it posible to replace a sub-class with its property using annotation?

推荐答案

这还不可能虽然有功能请求,但使用自定义序列化程序除外( http://jira.codehaus.org/浏览/ JACKSON-132 )可能会在杰克逊1.9中出现(因为它有相对较高的投票数,这意味着一些用户真的希望看到它实现)。

This is not yet possible except by using custom serializer, although there is a feature request ( http://jira.codehaus.org/browse/JACKSON-132 ) that might make it in Jackson 1.9 (as it has relatively high vote count, meaning a few users would really like to see it implemented).

这篇关于内部类的自定义杰克逊映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆