Thread
#serialization
    Stylianos Gakis

    Stylianos Gakis

    2 months ago
    I got an interface
    Action
    with normal subclass
    class SomeAction : Action
    and another interface subclass of type
    interface ServerAction : Action
    which itself has implementations like
    class SpecificServerAction : ServerAction
    . I’ve been trying to think of how I need to setup my SerializersModule to make all of this work. From what I understand since my interface is not sealed, I need to do something like this
    polymorphic(Action::class) {
        subclass(ServerAction::class, //??)
        subclass(NavigationPushAction::class, NavigationPushAction.serializer())
        subclass(NavigationPopAction::class, NavigationPopAction.serializer())
        subclass(VoidAction::class, VoidAction.serializer())
    }
    but how do I do this for the ServerAction? Optimally I’d like to be able to define my ServerAction hierarchy the same way by providing all the subclasses, but I don’t see a way that the dsl allows me to do this? I could probably do this by creating my own
    object ServerActionSerializer : KSerializer<ServerAction> {
    and implementing it, but I’d rather not if possible? I like this subclass approach which can take care of it for me. Am I missing something, any ideas maybe? Some part of the api I can be using that I am not?
    Emil Kantis

    Emil Kantis

    2 months ago
    Are you using the latest version of kotlinx-serialization? I can do
    polymorphic(Action::class,  SpecificServerAction::class, SpecificServerAction.serializer())
    Stylianos Gakis

    Stylianos Gakis

    2 months ago
    That would mean I’d then register a bunch of
    polymorphic
    calls if I have many implementations of
    ServerAction
    (which I do) Something like this:
    polymorphic(Action::class) {
        subclass(NavigationPushAction::class, NavigationPushAction.serializer())
        subclass(NavigationPopAction::class, NavigationPopAction.serializer())
        subclass(VoidAction::class, VoidAction.serializer())
    }
    polymorphic(Action::class, SpecificServerAction::class, SpecificServerAction.serializer())
    polymorphic(Action::class, Specific2ServerAction::class, Specific2ServerAction.serializer())
    polymorphic(Action::class, Specific3ServerAction::class, Specific3ServerAction.serializer())
    ... and so on
    And at no point in this structure am I referencing
    ServerAction
    itself. I would’ve expected I’d need to also add something like
    polymorphic(ServerAction::class) {
        subclass(SpecificServerAction::class, SpecificServerAction.serializer())
    }
    And then somehow tell
    polymorphic(Action::class)
    about it, since
    ServerAction
    is also a subclass of
    Action
    . But that’s not possible I suppose. In my mind that’s what would have made the most sense 😄 Tell Action about all of its subclasses. Tell ServerAction about all of its subclasses as well. And then inform Action that ServerAction is one of them too.
    e

    ephemient

    2 months ago
    polymorphic(Action::class) {
        subclass(SpecificServerAction::class, SpecificServerAction.serializer())
    }
    polymorphic(ServerAction::class) {
        subclass(SpecificServerAction::class, SpecificServerAction.serializer())
    }
    Stylianos Gakis

    Stylianos Gakis

    2 months ago
    Aha so I have to duplicate the subclasses since they're technically both implementations of Action and of ServerAction at the same time. What's with the different syntax between the two, why specify the serializer in one but not the other? Is it just because I can skip it, or is it that I must skip it? If it's just a can, I'd rather keep it consistently the same 😅
    e

    ephemient

    2 months ago
    ah, failure to copy-paste. there is a reified helper so you shorten the call arguments, but it's not "must"