Hello, World! I have a sealed class `Event` and am...
# serialization
b
Hello, World! I have a sealed class
Event
and am using
@JsonClassDiscriminator
successfully for its subtypes. But now I have one of the subtypes (
Message
) that should itself be a sealed class with a few sub-subtypes:
Copy code
┌─────┐
        │Event│  discriminator: "type"
        └──▲──┘
┌─────┐    │   ┌───────┐
│React├────┴───┤Message│  discriminator: "subtype"
└─────┘        └───▲───┘
          ┌───┐    │    ┌────┐
          │Add├────┴────┤Edit│
          └───┘         └────┘
Naturally I'd put another
@JsonClassDiscriminator
on
Message
but that's not allowed. Any tip on how I should handle this?
d
Why is it not allowed?
b
I'm not sure what the reason is, but the doc says:
It is not possible to define different class discriminators for different parts of class hierarchy.
and if you actually try it in the IDE it will be red 🙂
d
What happens if you ignore the IDE and run the code?
b
haven't tried to be honest...
but the documentation seems pretty clear about it
d
Indeed. You might need to make
Message
a concrete class then, then have a polymorphic member.
b
I did something similar currently, but it's not very satisfactory - I'd love to have a class hierarchy that looks like the API one
d
Though thinking about it, I suppose it'd be rather tricky to serialize something like this.
Copy code
val event: Event = Add()
Since there isn't a way to figure out the
Message
class in between.
You could consider using a single discriminator for the whole hierarchy.
b
You could consider using a single discriminator for the whole hierarchy.
Do you mean a change in the backend? (it's out of my hands, this is actually for the Slack API)
d
I see. At this point your best best is to create an issue and/or custom serializer.
b
I saw a bunch of issues about the topic but not sure they were exactly like what I'm trying to do 🙂 I think I'll open one then. Thanks a lot for replying!