Can I configure Jackson-Json Mapper to exclude properties based on which it is serialized?

Suppose I have objects, such as Business with a list of address objects and an Order that has a business.

Is it possible to configure the list of addresses from the Business object when ordering, and when the business is serialized, it includes a list?

I use ajax to retrieve data for the RIA, and when working with Order I really do not need these addresses, but when working with Business I need a list.

I also use Hibernate to save, so this is a really efficient and effective optimization.

+7
java json jackson
source share
4 answers

If I understood the question correctly, yes, I think JSON Views for Jackson would allow this. Basically you will create two different views (profiles) for the same type and choose which one to use for serialization.

+8
source share

You can use JsonIgnore Annotation to ignore the address list in serialization and disable the use of annotations in the ObjectMapper SerializationConfig when serializing the business. Of course, this means that other annotations you can use are also ignored. Not perfect, but you can find a better solution considering this, hoping this helps (obviously).

ObjectMapper mapper = new ObjectMapper(); mapper.getSerializationConfig().disable(Feature.USE_ANNOTATIONS); 
+5
source share

Yes you can do it. All you need to do is declare the list of objects as a transient property in the business object.

Then add the following code to your jsonConfig:

 jsonConfig.setIgnoreTransientFields(true); 
0
source share
 @JsonIgnore 

used to ignore properties that you do not want to convert to json.

 public class UserDocument { private long id; private String documentUrl; @JsonIgnore private byte documentType; //traditional getters and setters } 

Conclusion: this converts the id and documentUrl properties, but does not translate the documentType property.

 { "id": 5, "document_url": "/0/301115124948.jpg" } 
-one
source share

All Articles