https://kotlinlang.org logo
#serialization
Title
# serialization
t

Tech

02/27/2023, 1:30 AM
Hey I was wondering how I would handle this interaction. I have an API written in TypeScript that returns a
data
field where in Kotlin I serialize into this basic object
Copy code
@Serializable
data class PayloadData<T>(
    val error: Boolean,
    val data: T? = null
)
However, sometimes when no data needs to be sent back the data field sent back is undefined, and I try to set the type of PayloadData to Void and kotlinx serialization fails, was wondering if there was a way to handle this case?
e

ephemient

02/27/2023, 1:54 AM
kotlinx.serialization 1.5.0 includes a "serializer" for
Nothing
, so
PayloadData<Nothing>(data = null)
should work
t

Tech

02/27/2023, 1:55 AM
Oh really? I thought Nothing represented no operation was done and would just throw an error, I’ll try it out when I can though
e

ephemient

02/27/2023, 1:57 AM
there are no values of type
Nothing
, but there is
null: Nothing?
t

Tech

02/27/2023, 1:58 AM
ah gotcha just use nothing as a sort of proxy to serialize a null
6 Views