Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include only certain properties and fields of my object to be serialized by Jackson?

I have a class I need to serialize and it contains a lot of properties and fields, but I only need a small subset to be serialized, not the entire class. I also want to guarantee that I won't inadvertently add fields in the future which would be serialized and break my expectation of what gets serialized by suddenly including something new.

So is there any annotation I can use to basically specify what gets serialize only?

I'd need like the opposite of @JsonIgnoreProperties. Something that acts as a whitelist instead of a blacklist.

like image 855
Didier A. Avatar asked Jan 07 '15 19:01

Didier A.


People also ask

Does Jackson serialize private fields?

Jackson can't serialize private fields - without accessor methods - with its default settings.

How can you avoid certain member variables of class to be serialized?

You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.

How do you tell Jackson to ignore a field during serialization?

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.


1 Answers

Haven't tested this, but you should be able to disable JsonAutoDetect, then annotate each property manually.

See http://wiki.fasterxml.com/JacksonFeatureAutoDetect

like image 65
Neil McGuigan Avatar answered Oct 09 '22 02:10

Neil McGuigan