I want to modify existing data class, by moving one of its parameters into second data class and I wonder what's the best way to handle it, without loosing the data already stored using the old data class. Data is stored using Json serializer and app has mechanisms for updating data stored in databases on app's update.
Here's example:
Currently used data class
@Serializable
@Parcelize
data class AlarmInfo (
val volume: Int
(...)//other fields
) : Parcelable
Data classes to be used after app's update
@Serializable
@Parcelize
data class AlarmInfo (
val ringtoneInfo: RingtoneInfo
(...)//other fields
) : Parcelable
@Serializable
@Parcelize
data class RingtoneInfo (
val volume: Int
(...)//other fields
) : Parcelable
Is there a way to retrieve AlarmInfo.volume
stored value, when AlarmInfo
class after update doesn't have this field anymore?
I know I can do it ugly way, by keeping the AlarmInfo.volume
in the new class and copy its value to the RingtoneInfo.volume
on app's update, but it doesn't feel right to keep this field forever after this one update.
Any suggestions?
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?
I am not aware of any annotation that would do that for you, except GSON's (and other parsers' equivalent) @SerializedName
E.g.:
@SerializedName("name")
var userName: String
Keep in mind if you use Proguard, you need to tell it to keep the model class, or it may be obfuscated into something else. Of course, that's only a GSON annotation for the purpose of serialization and deserialization.
Other than that, you could hide the field behind a function or get()
like
@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
(all pseudo code, you get the idea). Or similar ideas...