Kotlinx.serialization: Can I omit discriminator field/array?

Created on 21 May 2019  路  7Comments  路  Source: Kotlin/kotlinx.serialization

I need to serialize some data that I will never deserialize with kotlinx.serialization.
This data has a value of an interface type.
I don't want a discriminator field like "type" or anything like it.

Also, ideally I shouldn't have to define a polymorphic mapping in SerializersModule since I'm just serializing and not deserializing data. When deserializing you have to have a discrimator and a mapping to the serializers. However, when serializing you don't need this map since whatever the sub-object is the "attached" serializer is the correct one. - Unless this must not be accomplish polymorphically.

For now I have to make my one class into a suite of classes. One for each implementation of the interface.

feature

Most helpful comment

Basically the easiest thing to do is to write a custom serializer. It can write the values however it pleases without using polymorphic serialisation

All 7 comments

Since this library does not use reflection (especially on JS/Native), you still need to use SerializersModule to provide stable KClass -> KSerializer mapping, because in polymorphic serialization KSerializers can't be known in advance just by looking at the source code.

@sandwwraith What about omitting the field? How is that done?

P.S: I didn't see this and created a duplicate #548 , closing that in favor of this

@karuppiah7890 . I think it's not possible for now. By omitting the field, it would be impossible to deserialize data back. What's your use case for that? Serialization only?

@sandwwraith You are right, I just wanted to serialize, no deserialization. The use case was to create a JSON body for the Hangouts API https://developers.google.com/hangouts/chat/reference/message-formats/cards and I created data classes for it but got stuck with the extra type field and some null values. I used custom serializer for getting rid of the type field and used encodeDefaults false to get rid of null values.

Here's the final code in case anyone wants to use it for reference https://github.com/susmithasrimani/gocd-google-chat-build-notifier/blob/master/src/main/kotlin/io/github/susmithasrimani/gocd/googleChat/chatMessage/ChatMessage.kt

and I used this medium article : https://medium.com/transferwise-engineering/how-to-master-polymorphism-and-custom-serializers-in-kotlinx-serialization-7190da0f42aa

@sandwwraith @karuppiah7890
So is there anyway to remove the field? In our use case we don't need to de-serialize. We are just taking an object and sending it to a JSON API which simply responds with success or fail.
However it doesn't expect the "type" field so it always fails.

Basically the easiest thing to do is to write a custom serializer. It can write the values however it pleases without using polymorphic serialisation

You can filter the json keys and remove the type:

val json = Json.encodeToJsonElement(myObject)
val valuesWithoutType = json.jsonObject.filterNot { it.key == "type" }
val jsonTrimmed = Json.encodeToJsonElement(valuesWithoutType)
Was this page helpful?
0 / 5 - 0 ratings