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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2507768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:33:04  来源:igfitidea点击:

Possible to configure Hymanson-Json Mapper to exclude properties based on which object it is serializing?

javajsonHymanson

提问by Robby Pond

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.

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

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

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

回答by StaxMan

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

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

回答by Wolfram

You could use the JsonIgnore Annotationto ignore the Addresslist in serialization and switch off the use of annotations in the ObjectMapper's SerializationConfigwhen 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).

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

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

回答by user2294458

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 中:

jsonConfig.setIgnoreTransientFields(true);

回答by Abdullah Khan

@JsonIgnore 

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

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

public class UserDocument {

    private long id;

    private String documentUrl;

    @JsonIgnore
    private byte documentType;

    //traditional getters and setters
}

Output: This will convert the properties idand documentUrlbut will not convert property documentType.

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

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