I have a type hierarchy made of interfaces. I also...
# serialization
z
I have a type hierarchy made of interfaces. I also have some data classes at the deepest point. Is there a way to easily or quickly register all of the data classes as all of the interfaces?
It is larger, wider, and deeper than this, but this is a small example.
Copy code
Top
- Middle
  - DeeperMiddle
    - FirstDataClass
    - SecondDataClass
    - EvenDeeperMiddle
      - ThirdDataClass
- AlsoMiddle
  - FourthDataClass
To outline, I want the following registrations. I figure I could do this with a list of classes when building the SerializersModule, just curious if there's a better way. • Top: FirstDataClass, SecondDataClass, ThirdDataClass, FourthDataClass • Middle: FirstDataClass, SecondDataClass, ThirdDataClass, FourthDataClass • DeeperMiddle: FirstDataClass, SecondDataClass, ThirdDataClass • EventDeeperMiddle: ThirdDataClass • AlsoMiddle: FourthDataClass
e
you could write a KSP processor to generate
Copy code
val serializersModule: SerializersModule = SerializersModule {
  polymorphic(Top::class) {
    subclass(FirstDataClass.serializer())
    subclass(FourthDataClass.serializer())
    subclass(SecondDataClass.serializer())
    subclass(ThirdDataClass.serializer())
  }
  polymorphic(Middle::class) {
    subclass(FirstDataClass.serializer())
    subclass(FourthDataClass.serializer())
    subclass(SecondDataClass.serializer())
    subclass(ThirdDataClass.serializer())
  }
  polymorphic(DeeperMiddle::class) {
    subclass(FirstDataClass.serializer())
    subclass(SecondDataClass.serializer())
    subclass(ThirdDataClass.serializer())
  }
  polymorphic(EvenDeeperMiddle::class) {
    subclass(ThirdDataClass.serializer())
  }
  polymorphic(AlsoMiddle::class) {
    subclass(FourthDataClass.serializer())
  }
}
for you
although what we actually do in the codebase of my largest project is to write the
SerializersModule
by hand, and have a unit test which scans the classpath for subclasses of interfaces of interest, and checks that they're registered
z
Thanks so much for this @ephemient I'll see what I can come up with, but I was kind of leaning towards hand made as well