简体   繁体   English

在gson序列化(Java)期间动态排除某些属性

[英]Dynamically exclude certain attributes during gson serialization (Java)

Is there a way to dynamically exclude certain attributes of a class during gson serialization? 有没有办法在gson序列化过程中动态排除类的某些属性? (Based on a parameter I pass to the writer) (基于我传递给编写器的参数)

Or should I make 2 different serializers? 还是应该制作2个不同的序列化器?

Thanks! 谢谢!

Try using transient attribute. 尝试使用transient属性。 Ex: private transient String name; 例如: private transient String name;

Also you can add this property 您也可以添加此属性

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

Edit: Take a look here 编辑: 在这里看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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