I want to dynamically include or exclude a field in a kotlin class based on certain conditions at run time before making a POST request using Retrofit and Gson, anyone has ideas on how to achieve this dynamically? I've tried using the expose annotation but it can't accept run time variables.
w
wasyl
04/03/2020, 8:38 AM
Make it nullable and configure type converter (somehow) to not serialize null fields maybe?
j
Jabez Magomere
04/03/2020, 9:05 AM
@wasyl i have a couple of instances where i may want to send null fields to the backend.
w
wasyl
04/03/2020, 9:08 AM
You could try with a wrapper type then, something like
Optional<T?>?
, and have a custom type adapter for it
wasyl
04/03/2020, 9:09 AM
Such that if
optionalValue = null
you omit it, but if it’s
optionalValue = Optional.of(null)
then you send it
j
Jabez Magomere
04/03/2020, 9:17 AM
@wasyl would be a good solution, the only problem is that this only works on min API level 24 and i'm working on 22