定位嵌套属性的Jackson @JsonIgnoreProperties [英] Jackson @JsonIgnoreProperties targeting nested attributes

查看:294
本文介绍了定位嵌套属性的Jackson @JsonIgnoreProperties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使@JsonIgnoreProperties以嵌套属性为目标?

Is there a way to make @JsonIgnoreProperties target nested attributes?

类似于以下代码:

public class ParentObject() {
    @JsonIgnoreProperties({ "subAttributeA.subAttributeB.subAttributeC" })
    private ChildObject attribute;
}

在此示例中,我希望在 ParentObject 的序列化中不包含 subAttributeC -但仍需要将同一 subAttributeC 在其他情况下可序列化.

In this example, I want that subAttributeC is not included in the serialization of a ParentObject - but this same subAttributeC still need to be serializable in other scenarios.

如果无法使用注释,该如何实现?

If not possible with annotations, how to achieve this?

推荐答案

一种可能的方法是将@JsonSerialize与自定义序列化程序一起使用.这在较低的抽象级别上运行,并且您基本上必须指定如何序列化每个单个属性.在此处的示例中,该示例使用一个平面对象,但考虑到JsonGenerator具有方法像writeObject等,我确定它也可以用于层次结构.

One possible way is to use @JsonSerialize with a custom serializer. This operates on a low abstraction level and you basically have to specify how to serialize every single attribute. There's an example here that uses a flat object, but given that JsonGenerator has methods like writeObject etc., I'm sure this can be used for a hierarchical structure, too.

这篇关于定位嵌套属性的Jackson @JsonIgnoreProperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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