简体   繁体   中英

How to exclude certain attributes from being serialized

This question has been asked multiple time, I have a Java POJO class which I would like to serialize by excluding some attributes. In order to do this, I am using @Expose from GSON. The problem is that it does not seem to work.

Even if I use this: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

It does not work.

I am reluctant to use transient because it disables both serialization and deserialization of the given attribute.

you can use transient

private transient String property;

Make sure you initialize your Gson object properly. You should call excludeFieldsWithoutExposeAnnotation method in your GsonBuilder if you want to use the Exclude annotation.

For example Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

Gson javadoc description of excludeFieldsWithoutExposeAnnotation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM