我正在为许多第三方 API 生成 JSON。他们中的许多人接受不同对象的列表(JSON 数组),但是,他们都不会接受 "type": "com.mycom.someclass"
由于列表的多态性,由 kotlinx 序列化自动生成。
在我的用例中,我只关心序列化。不需要反序列化。
当前的:
[{"type":"com.mycom.Child1","x":"child1"}, {"type":"com.mycom.Child2","y": 23}]
需要:
[{"x":"child1"}, {"y": 23}]
如何禁用此自动行为?
最佳答案
看看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.
您需要输入
JsonParametricSerializer
并手动选择序列化程序。由于您不需要支持反序列化,因此实现将是微不足道的。
关于kotlin - 如何禁用 kotlinx 序列化多态鉴别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62053677/