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.
Jackson can't serialize private fields - without accessor methods - with its default settings.
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.
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.
Haven't tested this, but you should be able to disable JsonAutoDetect, then annotate each property manually.
See http://wiki.fasterxml.com/JacksonFeatureAutoDetect
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With