最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

serialization - In kotlin, how to rename a @Serializable class while maintaining backward compatibility? - Stack Overflow

programmeradmin0浏览0评论

The old class name is:

@Serializable
@SerialName("OldClass")
class OldClass(
    val member: String,
) : IClass {
...
}

Now I want to rename it to:

@Serializable
@SerialName("NewClass")
class NewClass(
    val member: String,
) : IClass {
...
}

The problem is, users might have already serialized and saved it. So, what I want is:

  • When serializing, use the NewClass
  • When deserializing, no matter if it reads OldClass or NewClass, always decode with NewClass

The SerializeModule that used for serializing/deserliazing looke like:

val serializeModule = SerializersModule {
    polymorphic(IClass::class) {
        subclass(OldClass::class) 
        subclass(NewClass::class)

        ...
    }
}

Currently, I need to implement both classes, which are exactly the same, is it possible to only implement the NewClass, and use an alias for the old one?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论