Support multiple class descriminators for polymorphism.
See original GitHub issueWhat is your use-case and why do you need this feature?
As of writing this issue, only a single classDiscriminator
can be specified on a Json
object.
I’m working with a REST api where for different endpoints or error codes, the object returned will use a different “class discriminator”.
For example, if the response code is != 200, then an error in form of JSON is returned.
The discriminator here is errcode
.
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_LIMIT_EXCEEDED")
data class LimitExceeded(
override val error: String,
@SerialName("retry_after_ms")
val retryAfterMillis: Long
) : MatrixError() {
override val errcode get() = "M_LIMIT_EXCEEDED"
}
If the response code is 200, then the model uses type
as the discriminator.
sealed class Event
@SerialName("m.room")
data class RoomEvent : Event
@SerialName("m.join")
data class JoinEvent(val userId: String) : Event
Describe the solution you’d like I would like to be able to annotate the parent class with a discriminator.
Or supply a class discriminator in the module like so.
SerializersModule {
polymorphic<MatrixError>(discriminator="errcode") {
addSubclass(MatrixError.Unknown.serializer())
addSubclass(MatrixError.NotFound.serializer())
addSubclass(MatrixError.Forbidden.serializer())
addSubclass(MatrixError.UnsupportedRoomVersion.serializer())
addSubclass(MatrixError.LimitExceeded.serializer())
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:53
- Comments:19 (10 by maintainers)
Top Results From Across the Web
Polymorphism - GitHub Pages
When you have a class hierarchy and will be serializing instances of varying classes to the same collection you need a way to...
Read more >Polymorphism — PynamoDB 5.3.4 documentation
PynamoDB supports polymorphism through the use of discriminators. A discriminator is a value that is written to DynamoDB that identifies the python class...
Read more >Kotlin serialization: nested polymorphic module - Stack Overflow
Recently I've been trying to implement Kotlinx Serialization for polymorphic class hierarchy. I used this guide, however my example was a ...
Read more >Inheritance and Polymorphism - Swagger
To help API consumers detect the object type, you can add the discriminator/propertyName keyword to model definitions. This keyword points to the property ......
Read more >Mapping Class Inheritance Hierarchies — SQLAlchemy 1.4 ...
While a polymorphic discriminator expression is not strictly necessary, it is required if polymorphic loading is desired. Establishing a simple ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Will this be included in the next release? I’m really looking forward to it.
@KamilJanda It is planned and in progress now