New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't register both a default serializer and deserializer #1837
Comments
I don't see why you need to use You can just do this. @Serializable(PacketJsonSerializer::class)
sealed class Packet |
@Dominaezzz how would that work if I'd like to serialise/deserialise |
Oh, I missed that requirement. |
I haven't got that far yet, because of the bug :) I think I tried contextual serialization, but it didn't work because that is only for determining fields. It didn't work for |
@aSemy The way I would do it is is the way I have some special types/serializers for the XML context (like serializing document fragments directly). Basically it is perfectly valid to check the type of the encoder/decoder in the encode and decode functions and handle the json coders specially. Just do the "normal" thing for other formats, unless you want to special case those too. Note that this is special casing with a normal fallback so shouldn't really have a problem with using runtime types. From my perspective this is the only really sane way to do it in that it doesn't put any burden upon the users of the types/serializers or upon formats/the serialization library. |
@pdvrieze I don't understand what you're trying to explain. I'm not using XML. I need to use a SerializersModule to define format-specific serialization/deserialization strategies so the DTOs aren't restricted to a specific format, but there's a bug. It looks like the cause has been identified and fixed in #1849. |
@aSemy The mention of XML is just as an example. What you want to do is something like (just using object MyCustomSerializer: KSerializer<Long> {
override val descriptor: SerialDescriptor = TODO()
override fun deserialize(decoder: Decoder): Long = when(decoder) {
is JsonDecoder -> decoder.decodeString().toLong()
else -> decoder.decodeLong()
}
override fun serializer(encoder: Encoder, value: Long): Unit = when (encoder) {
is JsonEncoder -> encoder.encodeString(value.toString())
else -> encoder.encodeLong(value)
}
} |
Hi @sandwwraith, thanks for fixing it. I'd like to use it, is there a plan for releasing a new version? Or is there a nightly version I can use? |
how can i use the merged solutions? |
Fix is available in the new 1.4.0-RC version |
I want to register a polymorphic KSerializer in a
SerializersModule
. When encoding/decoding with JSON, I want to discriminate differently to when I am encoding in another format.(I am trying to find a work around for #1664 - the non-accessible default 'type' field disrupts other systems.)
I've tried various combinations of registering serializers - with an annotation, in the serializer module using polymorphic or contextual - but it seems this isn't possible.
(aside: it looks like there's a typo in the error message, 'class' appears twice: "class class Packet")
Expected behavior
Encoding/decoding with discriminators should be format-independent, and should not require hidden/non-accessible type fields.
Environment
The text was updated successfully, but these errors were encountered: