I have a number of subclasses of a class called `B...
# serialization
t
I have a number of subclasses of a class called
BytesEq
, which just adds some equality semantics on top of a ByteArray, as well as an intermediary class
BytesSized
that adds init-time checking that the byte array is of an appropriate size. Is there a way I can generate serializers for all of these with one bit of code, or is each one going to need a separate custom serializer? I'm having trouble sorting through the options. • BytesEq and BytesSized: https://codeberg.org/cavern/spelunk/src/commit/c2767e38ce7ac23785eb87c06a2dc5c9b3cda51f/src/main/kotlin/org/timmc/spelunk/Utils.kt#L641 • Various subclasses of those: https://codeberg.org/cavern/spelunk/src/commit/c2767e38ce7ac23785eb87c06a2dc5c9b3cda51f/src/main/kotlin/org/timmc/spelunk/Crypto.kt#L565
solved 1
So for example I've got
class CatalogFinderEntry(bytes: ByteArray): BytesEq(bytes)
-- this just adds type safety and equals/hash/toString. But I really just want to use a ByteArraySerializer any time it (or any BytesEq subclass) shows up as a field in some other class. Is that doable?
Looks like the answer is no, because each class needs its own serializer class. I've filed a feature request issue for this: https://github.com/Kotlin/kotlinx.serialization/issues/2659
And... solved! A common serializer class can be subclassed. See comments. I guess I've been away from Kotlin for too long and have forgotten how to work with inheritance. 😆