Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

json

jackson

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.

like image 500
Robby Pond Avatar asked Mar 24 '10 12:03

Robby Pond


People also ask

How do you ignore certain fields based on a serializing object to JSON?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

How do I ignore JSON property based on condition?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do I ignore properties in Jackson?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.


1 Answers

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.

like image 64
StaxMan Avatar answered Oct 01 '22 08:10

StaxMan