Is having a `classDiscriminator` per parent class ...
# serialization
d
Is having a
classDiscriminator
per parent class planned?
s
Can you explain in details, please?
d
At the moment, there's a
classDiscriminator
for the
Json
object. So all polymorphic sealed classes have to use the same one. The rest API I'm consuming (with ktor) returns different "classDiscriminators". In some cases it's "errcode", sometimes it's "type" or even a composite "subtype". I'll provide more concrete examples.
So now I have,
Copy code
sealed class MatrixError : Throwable() {
    abstract val errcode: String
}

@Serializable
@SerialName("M_NOT_FOUND")
data class NotFound(override val error: String) : MatrixError() {
    override val errcode get() = "M_NOT_FOUND"
}

@Serializable
@SerialName("M_UNKNOWN")
data class Unkown(override val error: String) : MatrixError() {
    override val errcode get() = "M_UNKNOWN"
}

@Serializable
@SerialName("M_LIMIT_EXCEEDED")
data class LimitExceeded(
    override val error: String,

    @SerialName("retry_after_ms")
    val retryAfterMillis: Long
) : MatrixError() {
    override val errcode get() = "M_LIMIT_EXCEEDED"
}
In this case the
classDiscriminator
is
errcode
when deserializing. (I also want access to the
classDiscriminator
after deserialising but that's a differeny problem).
I also have,
Copy code
sealed class Event

@SerialName("m.room")
data class RoomEvent : Event

@SerialName("m.join")
data class JoinEvent(val userId: String) : Event
In this case the
classDiscriminator
is
type
. (I don't need access here.)
I'd have to use different
Json
object to parse the sealed classes and as a consequence, two `HttpClient`s and in some cases I just can't, since the same request could return either sealed class type.
I might also need to support this soon.
Copy code
sealed class Event

@SerialName("m.room")
sealed class RoomEvent : Event

@SerialName("exit")
data class RoomExitEvent: RoomEvent

@SerialName("m.join")
data class JoinEvent(val userId: String) : Event
Bad names but you get the idea. Each subclass that is a parent, would get their own
classDiscriminator
.
You can have a looksie at the api spec https://matrix.org/docs/spec/client_server/r0.5.0#id278. If I haven't done justice to the explanation.