How do I make this polymorphically serializable? ...
# announcements
a
How do I make this polymorphically serializable?
Copy code
@Serializable
sealed class Result<out T> {
    @Serializable
    class Success<out T>(val data: T) : Result<T>()

    @Serializable
    class Failure<out T>(val msg: String) : Result<T>()

    companion object {
        private fun <T> resultContext(serializer: KSerializer<T>) = SerializersModule {
            polymorphic<Result<T>> {
                subclass(Success.serializer(serializer))
                subclass(Failure.serializer(serializer))
            }
        }

        fun <T> stringify(serializer: KSerializer<T>, res: Result<T>): String {
            return Json(context = resultContext(serializer)).stringify(
                Result.serializer(serializer),
                res
            )
        }

        fun <T> parse(serializer: KSerializer<T>, json: String): Result<T> {
            return Json(context = resultContext(serializer)).parse(
                Result.serializer(serializer),
                json
            )
        }
    }
}}
So that If I have a class
Copy code
@Serializable
data class Person(val name:String)
I can call
Copy code
Result.stringify(Result.Serializer(Person.serializer()),Result.Success(Person("Andy))) // This says class Person is not registered for polymorphic serialization
I have little knowledge in Polymorphic Serialization. Also, I need to be able to serialize a
Result.Success<Person?>(null)
. Currently I don't know how to do so. Help?
k
I’m not familiar with it either but it seems that you need to modify your stringily function like so (change the Result.serializer(serializer))
Copy code
fun <T> stringify(serializer: KSerializer<T>, res: Result<T>): String {
    return Json(context = resultContext(serializer)).stringify(
            PolymorphicSerializer(Result::class),
            res
    )
}
but then parse doesn’t work and not sure how to get that working. should be the same for parse just have to cast it
a
I tried that already, I get the same error. Person is not registered for serialization
k
oh sorry also change your call to stringify to
Copy code
Result.stringify(Person.serializer(), Result.Success(Person("Andy")))
this is what i’m using
Copy code
@Serializable
sealed class Result<out T> {
    @Serializable
    class Success<out T>(val data: T) : Result<T>()
    @Serializable
    class Failure<out T>(val msg: String) : Result<T>()
    companion object {
        private fun <T> resultContext(serializer: KSerializer<T>) = SerializersModule {
            polymorphic<Result<T>> {
                subclass(Success.serializer(serializer))
                subclass(Failure.serializer(serializer))
            }
        }
        fun <T> stringify(serializer: KSerializer<T>, res: Result<T>): String {
            return Json(context = resultContext(serializer)).stringify(
                    PolymorphicSerializer(Result::class),
                    res
            )
        }
        fun <T> parse(serializer: KSerializer<T>, json: String): Result<T> {
            return Json(context = resultContext(serializer)).parse(
                    PolymorphicSerializer(Result::class),
                    json
            ) as Result<T>
        }
    }
}

@Serializable
data class Person(val name: String?)

fun main(args: Array<String>) {
    val jsonData = Result.stringify(Person.serializer(), Result.Success(Person("hey")))
    println(jsonData)

    when(val data = Result.parse(Person.serializer(), jsonData)) {
        is Result.Success -> println(data.data)
        is Result.Failure -> println(data.msg)
    }
}
a
I find out that, I am still getting the same error, but If I add
Copy code
polymorphic<Any> {
  subclass(serializer)
}
in the
SerializersModule
Then the error goes away. But again, I can't serialize
Result.Success<Person?>(null)