I want to dynamically include or exclude a field i...
# android
j
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
Make it nullable and configure type converter (somehow) to not serialize null fields maybe?
j
@wasyl i have a couple of instances where i may want to send null fields to the backend.
w
You could try with a wrapper type then, something like
Optional<T?>?
, and have a custom type adapter for it
Such that if
optionalValue = null
you omit it, but if it’s
optionalValue = Optional.of(null)
then you send it
j
@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
w
Then create your own wrapper 🤷‍♂️