处理应用程序更新中 Kotlin 数据 class 字段的删除,而不会丢失其值
Handle removal of Kotlin data class's field on app's update without loosing its value
我想修改现有数据 class,方法是将其中一个参数移到第二个数据 class 中,我想知道处理它的最佳方法是什么,而不会丢失已经使用旧数据 class。数据使用 Json 序列化程序存储,应用程序具有在应用程序更新时更新存储在数据库中的数据的机制。
示例如下:
当前使用的数据class
@Serializable
@Parcelize
data class AlarmInfo (
val volume: Int
(...)//other fields
) : Parcelable
应用程序更新后要使用的数据class
@Serializable
@Parcelize
data class AlarmInfo (
val ringtoneInfo: RingtoneInfo
(...)//other fields
) : Parcelable
@Serializable
@Parcelize
data class RingtoneInfo (
val volume: Int
(...)//other fields
) : Parcelable
当 AlarmInfo
class 更新后不再有此字段时,有没有办法检索 AlarmInfo.volume
存储的值?
我知道我可以通过将 AlarmInfo.volume
保留在新 class 中并在应用程序更新时将其值复制到 RingtoneInfo.volume
来做丑陋的方式,但它不会感觉在这次更新后永远保留这个字段是对的。
有什么建议吗?
Is there maybe some annotation I could see to have the volume field deserialized, but named differently in the class? Something like:
@CleverAnnotation(name="volume") val dontUse: Int?
除了 GSON(和其他解析器的等价物)之外,我不知道有任何注释可以为您做到这一点 @SerializedName
例如:
@SerializedName("name")
var userName: String
请记住,如果您使用 Proguard,您需要告诉它保留 模型 class,否则它可能会被混淆成其他东西。当然,那只是一个GSON注解,用于序列化和反序列化。
除此之外,您可以将字段隐藏在函数后面或 get()
如
@Deprecated(
message = "This field is deprecated and will be removed in future versions",
replaceWith = ReplaceWith("newField"),
level = DeprecationLevel.WARNING
)
var oldField: String? = null
var newField get() = oldField
(都是伪代码,你懂的)。
或者类似的想法...
我想修改现有数据 class,方法是将其中一个参数移到第二个数据 class 中,我想知道处理它的最佳方法是什么,而不会丢失已经使用旧数据 class。数据使用 Json 序列化程序存储,应用程序具有在应用程序更新时更新存储在数据库中的数据的机制。
示例如下:
当前使用的数据class
@Serializable
@Parcelize
data class AlarmInfo (
val volume: Int
(...)//other fields
) : Parcelable
应用程序更新后要使用的数据class
@Serializable
@Parcelize
data class AlarmInfo (
val ringtoneInfo: RingtoneInfo
(...)//other fields
) : Parcelable
@Serializable
@Parcelize
data class RingtoneInfo (
val volume: Int
(...)//other fields
) : Parcelable
当 AlarmInfo
class 更新后不再有此字段时,有没有办法检索 AlarmInfo.volume
存储的值?
我知道我可以通过将 AlarmInfo.volume
保留在新 class 中并在应用程序更新时将其值复制到 RingtoneInfo.volume
来做丑陋的方式,但它不会感觉在这次更新后永远保留这个字段是对的。
有什么建议吗?
Is there maybe some annotation I could see to have the volume field deserialized, but named differently in the class? Something like:
@CleverAnnotation(name="volume") val dontUse: Int?
除了 GSON(和其他解析器的等价物)之外,我不知道有任何注释可以为您做到这一点 @SerializedName
例如:
@SerializedName("name")
var userName: String
请记住,如果您使用 Proguard,您需要告诉它保留 模型 class,否则它可能会被混淆成其他东西。当然,那只是一个GSON注解,用于序列化和反序列化。
除此之外,您可以将字段隐藏在函数后面或 get()
如
@Deprecated(
message = "This field is deprecated and will be removed in future versions",
replaceWith = ReplaceWith("newField"),
level = DeprecationLevel.WARNING
)
var oldField: String? = null
var newField get() = oldField
(都是伪代码,你懂的)。 或者类似的想法...