Quantcast
Viewing latest article 4
Browse Latest Browse All 5

Answer by toto2 for Gson Serialize field only if not null or not empty

It seems to me the problem is not with gson. Gson correctly keeps track of the difference between null and an empty string. Are you sure you want to erase that distinction? Are you sure all classes that use TestObject don't care?

What you could do if you don't care about the difference is to change the empty strings to null within a TestObject before serializing it. Or better, make the setters in TestObject such that an empty string is set to null; that way you define rigidly within the class that an empty string is the same as null. You'll have to make sure the values cannot be set outside the setters.


Viewing latest article 4
Browse Latest Browse All 5

Trending Articles