Is there a way to have a subclass serialized with ...
# serialization
a
Is there a way to have a subclass serialized with the custom serializer of its superclass, without having to declare
@Serializable(with= ... )
on every subclass?
I'd like to write some generic serialization support for tiny types, such that they get serialized as their contained primitive value and not objects in their own right. e.g.
Copy code
@Serializable(with=TinySerializer::class)
abstract class Tiny<out T> { abstract val value: T }

data class SortCode(override val value: String) : Tiny<String>()

val sortCode = SortCode("12-34-56")
assertEquals(Json.encodeToString(sortCode), "12-34-56")
j
Contextual + SerializerModule
a
So, it seems that there are two possible approaches to this, both of which are mildly clunky: 1.
@Serializable(with=...)
requires a class literal, which forces a custom serializer for every tiny type, because deserialization requires it to construct an instance of the concrete tiny type. 2. Contextual + SerializerModule, as suggested by @Javier, which requires every field the has a tiny type to be annotated with
@Contextual
, plus listing the serializer in a SerializerModule and passing the module to the Json formatter. This approach does give the option of having just one generic custom serializer because we can call a method and pass parameters to customize the way it works. Overall, I suspect option one is the least distasteful, as it keeps the boilerplate confined to the declaration of the tiny type. e.g.
Copy code
@Serializable(with=SortCodeSerializer::class)
data class SortCode(override val value: String) : Tiny<String>()
object SortCodeSerializer: TinySerializer<String, SortCode>(::SortCode, serializer())
Am I missing a smarter way of doing this?
j
if you are the class owner, you can define the serializer with the companion object so you havent to specific it
message has been deleted
👀 1
Maybe this is the better approach for you to avoid boilerplate
a
for larger types, perhaps so - the serializer is likely to be more unique
j
Yeah, really I don’t know if this approach let you can block external serializers too, I didn’t try it 🤔
a
what I'm really aiming for is find a way of using tiny types (perhaps not quite going as far as inline classes) in objects that I want to serialize to json as part of an api that I have to call. The idea is to make lots of them, so the serialization has to be extremely low-effort.
I'm in complete control of the code, but I don't want to make life too difficult for myself.
I suspect three lines of code for each type is borderline acceptable - remembering that that is three lines more than I would need without tiny types and I have to do that for nearly every property. I think I'll give it go for a bit and see how it works.
j
if you go for that, create templates to automatically create the companion object with it, so you got the serializer in a few seconds
a
ha - would you believe I am writing the intellij live template right at this moment! 😁
😂 1