简体   繁体   中英

Dynamically exclude certain attributes during gson serialization (Java)

Is there a way to dynamically exclude certain attributes of a class during gson serialization? (Based on a parameter I pass to the writer)

Or should I make 2 different serializers?

Thanks!

Try using transient attribute. Ex: private transient String name;

Also you can add this property

Gson gson = gsonBuilder.excludeFieldsWithModifiers(Modifier.TRANSIENT).create();

Edit: Take a look here

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