Hi, [Not directly related to http4k, please redire...
# http4k
g
Hi, [Not directly related to http4k, please redirect me if this is not relevant] I am looking for guidance to write a Polymorphic Moshi adapter using a Boolean field as TypeLabel. I scouted through the http4k repository to find any examples but in vain. I have a usecase where the adapter needs to be decided by the boolean type
isSuccessful
. A over simplified example: Success response:
Copy code
{
  "isSuccessful": true,
  "value": "Okay!"
}
Failure response:
Copy code
{
  "isSuccessful": false,
  "error_logs": {
    "order": 66
  }
}
All the examples I referred use
String
as TypeLabel. How can I use
boolean
field instead of
String
?
a
Indeed this isn't directly related to Http4k, but I think we can help anyway. I don't think you'll find an automatic way to do polymorphic serialization without the standard type label. But you can build a custom Moshi
JsonAdapter
to do it. See the Http4k SQSEventAdapter and registering it to a custom Moshi.
g
Thanks @Andrew O'Hara for the help. I had custom adapter as the last option, but looks like there is no way to leverage
PolymorphicJsonAdapterFactory
. Thanks for confirming!
👍 2