0

Is there any annotation on Gson for fields that I would like to be included/excluded from serialization if they have null value?

class Person {
String lastname;
String firstname;
String address
}

I would like the fields lastname and firstname to be always included on serialization independent on its values (null or not) and the field address to be included only when it is not null.

serializeNulls() doen't work for me because it includes all the fields.
new GsonBuilder().serializeNulls().create()

Petros Tsialiamanis
  • 2,718
  • 2
  • 23
  • 35
  • @pczeus No, .serializeNulls() serializes all the fields even if these fields are null. I would like to serialize only specific fields if they are null, not all of them – Petros Tsialiamanis Apr 30 '20 at 19:11
  • Does this help? https://stackoverflow.com/questions/13120354/excluding-certain-fields-from-serialization-based-on-value-in-gson – Smile May 01 '20 at 04:37

0 Answers0