Hakon Grotte
08/15/2023, 8:49 AMifEmpty
return statement work with Json.encodeToString
? If the return type lambda of ifEmpty
is different than original variable Kotlin resolves type to Any
, causing kotlinx.serialization.SerializationException: Serializer for class 'Any' is not found.
I want to do
Json.encodeToString(
myVariable.ifEmpty { ErrorResponse(404, "Path not found.") }
)
myVariable
is a List
with a different data class
than ErrorResponse, e.g. List<Person>. I have marked both data classes as Serializable.Loney Chou
08/15/2023, 10:00 AMval PathNotFound: ErrorResponse = ErrorResponse(404, "Path not found.")
val json = if (myVariable.isNotEmpty()) {
Json.encodeToString(myVariable)
} else {
Json.encodeToString(PathNotFound)
}
Hakon Grotte
08/15/2023, 2:26 PMifEmpty {}