I'm using Gson with retrofit. My server accepts null values so in the gson builder I have given
gsonBuilder.serializeNulls();
So that null values will not be ignored. But in some API
s I have a special case, where some of the fields should be present even is its's null, and some other fields should not serialize if it is null.
for example If I have a request class like
class Request {
String id;
String type;
}
and if I have a request where id=null
and type=null
, gson
should serialize it like:
{
id : null
}
Which means, if type is null it should ignore that field, but if id is null, it should present as null in the request.
Currently it is serializing like:
{
id : null,
type:null
}
since I have given gsonBuilder.serializeNulls();
. How can I deal with this special case?