Guillermo Mosse
Guillermo Mosse

Reputation: 462

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!

Upvotes: 0

Views: 677

Answers (1)

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

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

Upvotes: 4

Related Questions