Is there way to overrider json key that is respons...
# serialization
e
Is there way to overrider json key that is responsible for the sealed class serialisation pick up?
I wrote
JsonContentPolymorphicSerializer
it works great for deserialisation however, it doesn't write field during serialisation
e
Yeah, I know it. So the only way it is to write completely new serialiser in this case
What if I try to override descriptor? Or descriptor is only used to pickup correct serialiser and is not used during serialisation?
p
One Idea I have is to maybe utilize the JsonTransformingSerializer and rewrite the type field
Might be worth a shot
e
Super thank you!
It looks like really a hack. I probably will just write a staightforward serialiser and talk to BE people to ask use type for such cases
p
Seems to be some kind of a standard
e
Thank you!
p
@Eugen Martynov Look what I found in a PR I was reviewing 😛 https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#class-discriminator
e
🙂 Great!!! I still need ask for the consistency from backend people. Otherwise I have to find a way how to tweak Json for every specific case. Here is also interesting decision - it is rather connected to the deserialization and not class itself.
p
Yeah I’d make a very strong case for sticking to the convention
e
There is the way!
@JsonClassDiscriminator
p
That one is well hidden :)
e
And if you have consistency in your BE you can do it with
Copy code
Json {
   classDiscriminator = "out_type"
}