I have this class (simplified)
@Serializable(with = MetaAny.Serializr::class)
class MetaAny constructor(
val obj: Any,
val meta: String? = null) {
object Serializr: KSerializer<MetaAny> {
override val descriptor: SerialDescriptor =
buildClassSerialDescriptor("com.travisfw.MetaAny") {
// element("obj", isOptional = false, descriptor = ???)
}
override fun deserialize(decoder: Decoder): MetaAny {
TODO("not implemented") //function body template
}
override fun serialize(encoder: Encoder, value: MetaAny) {
TODO("not implemented") //function body template
}
}
}
MetaAny basically just stores metadata about anything. Other code manages serialization mechanisms that do not enter into the dependencies of this package, and uses MetaAny to just keep arbitrary metadata about the obj field, a serializable object, though not necessarily serializable with kotlinx.serialization. To be explicit here, I will repeat: the val obj
will be serialized by other code using a mechanism that this package will not refer to in its dependencies. But I want the serialized obj
field to be included in the serialized MetaAny
.
What do I do to make MetaAny serializable with kotlinx.serialization? If it would help, I could require serialization and deserialization functions in the constructor like so:
class MetaAny<S> constructor(
val obj: S,
val encFun: (S) -> okio.ByteString,
val decFun: (okio.ByteString) -> S,
val meta: String? = null) {
Or I could require the serialized version of obj be provided in any number of ways. …but still, how do I implement the SerialDescriptor of KSerializer.descriptor?