I am having trouble compiling a Serialization data class that is also a generic. I am very new to Android and Kotlin, but am an experienced iOS/Swift developer (don't hold it against me).
I am trying to set up a generic data class wrapper around a GraphQL Response that can return me either the T generic in a data field, or an error in the error field.
import kotlinx.serialization.Serializable
@Serializable
data class GraphQLResponse<T:Serializable> (
val errors : List<ErrorResponse>? = null,
val data : Map<String, T>? = null
) {
@Serializable
data class Location(
val line: Int? = 0,
val column: Int? = 0,
val sourceName: String? = null
)
@Serializable
data class ErrorResponse(
val locations: List<Location>? = null,
val errorType: String? = null,
val message: String? = null
)
}
Compiling this I get the following error:
java.lang.AssertionError: Couldn't load KotlinClass from /Users/brett/Development/Company/android/app/build/tmp/kotlin-classes/debug/com/company/company/network/GraphQLResponse.class; it may happen because class doesn't have valid Kotlin annotations
And also a bunch of warning around reflection.
Is what I am trying to do possible with Generics and Serialization? Do I need to write my own deserialisation methods ?
Any help and advice would be appreciated.