Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable kotlinx serialization polymorphic descriminator?

I'm generating JSON for a number of third-party APIs. Many of them accept a list (JSON array) of different objects, however, none of them will accept the "type": "com.mycom.someclass" automatically generated by kotlinx serialization due to the polymorphic nature of the list.

In my use-case, I only care about serializing out. No deserialization is needed.

Current:

[{"type":"com.mycom.Child1","x":"child1"}, {"type":"com.mycom.Child2","y": 23}]

Needed:

[{"x":"child1"}, {"y": 23}]

How can I disable this automatic behavior?

like image 976
Newbie Avatar asked May 27 '20 22:05

Newbie


1 Answers

Take a look at Json parametric polymorphic deserialization:

You also can serialize data with such serializer. In that case, either registered or default serializer would be selected for the actual property type in runtime. No class discriminator would be added.

You'll need to impelement JsonParametricSerializer and manually select the serializer. As you don't need to support deserialization, the implementation would be trivial.

like image 79
madhead - StandWithUkraine Avatar answered Nov 02 '22 15:11

madhead - StandWithUkraine