如何(DE)序列化生成数据类的实例
我需要(de)serialize word&从JSON序列化的某些数据类
es es是由Gradle插件生成的。通常,我只会使用诸如moshi或kotlinx.Serialization
之类的库,然后将适当的注释添加到我要序列化的类中是一个问题。
我想避免手动将生成的数据类的所有字段映射到我可以(DE)序列化或为所有这些
Data Class < /code>因此,我想知道是否还有另一种方法可以告诉例如
kotlinx.serialization
class是@serializable
而无需直接将注释放在班级本身。
或者,是否有一种更好的方法可以转换到字符串的实例的实例?
I need to (de)serialize to and from JSON some of the data class
es that are generated by a Gradle plugin. Normally I would just use a library like Moshi or kotlinx.serialization
and add the proper annotation to the class I want to serialize but, since this data class
es are autogenerated, this is a problem.
I would like to avoid manually to map all the fields of the generated data class
to some other class that I can (de)serialize, or to write a custom adapter for all these data class
so, I was wondering if there is another way to tell, for example, kotlinx.serialization
that a class is @Serializable
without having to put the annotation directly on top of the class itself.
Or, alternatively, is there a better way to convert to and from a string an instance of a generated data class
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
kotlinx.Serialization
支持为第三方类生成序列化。我们需要在@serializer
中使用forclass
参数,例如:您可以在官方文档中阅读更多: https://github.com/github.com/kotlin/kotlinx。 serialization/blob/master/doc/serializers.md#deriving-external-serializer-for-hother-kotlin-class-class-pershiormentiment
kotlinx.serialization
supports generating serializers for 3rd party classes. We need to useforClass
parameter in@Serializer
, for example:You can read more in the official documentation: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#deriving-external-serializer-for-another-kotlin-class-experimental