Hi! Is there a way to serialize sealed only object...
# serialization
d
Hi! Is there a way to serialize sealed only object classes without using type discriminator? In my use case, I want to save/load list of sealed singleton services to configuration file. Its possible, but it makes configuration file unnecessary verbose:
Copy code
@Serializable
sealed class Parser {
    abstract fun parse(text: String)
}

@Serializable
@SerialName("XParser")
object XParser: Parser() {
    override fun parse(text: String) {}
}

@Serializable
@SerialName("YParser")
object YParser: Parser() {
    override fun parse(text: String) {}
}

@Serializable
data class Settings(val parsers: List<Parser>)

fun main() {
    val settings = listOf(XParser, YParser)
    Json.encodeToString(settings)
}
Gives
{"parsers":[{"type":"XParser"},{"type":"YParser"}]}
that could be better represented as
{"parsers": ["XParser", "YParser"]}
(like enum serialiaztion) since every object will not have value, only type. I know that limitation exists because children of sealed class can be non-singleton, but is there a way to override it? Or is there a better way to do this?
a
I think the easiest way to do this would be to use an enum instead of separate objects for each type. Otherwise you'd have to write a custom serializer that would map an object to the serial name.