Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Related Rants
Question regarding android. I am having a problem with retrofit (I am using moshi converter factory) and hope that you can help.
Basically I have a screen with 3 checkboxes. User is able to select any of these checkboxes, and also user is allowed to select none of them.
When user doesn't select any checkboxes and click complete button, I send a PATCH request to backend with a model which contains 3 null values.
Problem here is that PATCH request which is being sent doesn't include any properties which have been nulled.
I spent some time researching why retrofit/moshi doesn't serialize nulled properties and I found a fix.
So I have this line
.addConverterFactory(MoshiConverterFactory.create(moshi))
Which I replaced with
.addConverterFactory(MoshiConverterFactory.create(moshi).withNullSerialization())
Now nulls are serialized and I am able to send a PATCH request model with nulled values. However now I'm facing another problem. Across my app I'm using only one retrofit client and I don't want to serialize nulls for all requests. Also I don't want to create another retrofit client.
How can I fix this problem? As far as I've researched it seems that I need to add an adapter with toJson() and fromJson() methods and then somehow enable nullSerialization only for that adapter. However I don't completely understand that solution and not even sure how to handle it.
question
android