是否可以配置 Jackson-Json Mapper 以根据正在序列化的对象排除属性?

发布于 2024-08-26 00:56:33 字数 205 浏览 8 评论 0 原文

假设我有一些对象,例如带有地址对象列表的企业,以及包含企业的订单。

是否可以进行配置,以便在订单序列化时从业务对象中排除地址列表,而在业务序列化时包含该列表?

我正在使用 ajax 为 RIA 提取数据,在处理订单时我并不真正关心地址数据,但在处理业务时我确实需要列表。

我还使用 Hibernate 来实现持久性,因此这确实是一种效率和性能优化。

Say I have objects such as a Business with a List of Address objects, and an Order that has a Business.

Is it possible to configure so that when the Order is serialized it excludes the list of addresses from the Business object, and when the business is serialized it includes the list?

I'm using ajax to pull data for an RIA and when working with the Order I don't really care about the address data, but when dealing with Business I do want the list.

I'm also using Hibernate for persistence so this is really an efficiency and performance optimization.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜警司 2024-09-02 00:56:33

如果我正确理解问题,是的,我认为 Jackson 的 JSON 视图 会允许这样做。
您基本上会为同一类型创建两个不同的视图(配置文件),然后选择使用哪一个进行序列化。

If I understand question correctly, yes, I think JSON Views for Jackson would allow this.
You would basically create two different views (profiles) for same type, and choose which one to use for serialization.

恬淡成诗 2024-09-02 00:56:33

您可以使用 JsonIgnore Annotation 来忽略序列化中的地址列表并关闭 ObjectMapper 的 SerializationConfig 序列化业务时。当然,这意味着您可能使用的其他注释也会被忽略。并不完美,但您可能会找到更好的解决方案来研究这个问题,希望它有帮助(显然)。

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().disable(Feature.USE_ANNOTATIONS);

You could use the JsonIgnore Annotation to ignore the Address list in serialization and switch off the use of annotations in the ObjectMapper's SerializationConfig when serializing a Business. Of course, this means that other annotations you might use are ignored as well. Not perfect, but you might find a better solution looking into this, hope it helps (obviously).

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().disable(Feature.USE_ANNOTATIONS);
゛清羽墨安 2024-09-02 00:56:33

是的,你可以做到。您所需要做的就是将地址列表声明为业务对象中的临时属性。

然后将以下代码添加到您的 jsonConfig 中:

jsonConfig.setIgnoreTransientFields(true);

Yes, you can do it. All you need is to declare the List of Address as transient property in you business object.

Then add the following code to your jsonConfig:

jsonConfig.setIgnoreTransientFields(true);
暗地喜欢 2024-09-02 00:56:33
@JsonIgnore 

用于忽略您不想转换为 json 的属性。

public class UserDocument {

    private long id;

    private String documentUrl;

    @JsonIgnore
    private byte documentType;

    //traditional getters and setters
}

输出:这将转换属性 iddocumentUrl,但不会转换属性 documentType

{
  "id": 5,
  "document_url": "/0/301115124948.jpg"
}
@JsonIgnore 

is used to ignore properties that you don't want to convert to json.

public class UserDocument {

    private long id;

    private String documentUrl;

    @JsonIgnore
    private byte documentType;

    //traditional getters and setters
}

Output: This will convert the properties id and documentUrl but will not convert property documentType.

{
  "id": 5,
  "document_url": "/0/301115124948.jpg"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文