Is it possible to add extension functions to a cla...
# serialization
b
Is it possible to add extension functions to a class that is serializable? I've been able to add them to
KSerializer<T>
, but I'd prefer to add an extension function to
T
, for example
c
I don’t believe there’s anything added to serializable classes that marks them generically as such. You’d have to create your own interface and apply it to all your serializable classes, and then put the extension function on that interface
b
Right. I was thinking something along the lines of the
KClass<T>
thing that kotlinx.serialization does, but I can't seem to get that to work
c
Perhaps adding a custom interface to the companion object of those classes?
b
I tried that. They don't seem to come with `companion object`s, unless you add them to each one
c
Nothing gets automatically applied to the classes that you could put an extension function on. The serialization plugin adds the
.serializer()
method only, but no other markers. You’ll have to do it yourself
👍 1