kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Support field renaming within parent classes/interfaces

Open benkuly opened this issue 4 years ago • 2 comments

What is your use-case and why do you need this feature? Currently, one need to rename every implemented field of a parent class/interface:

interface Project {
    val name: String
    val originalTime: Long
}

@Serializable
class OwnedProject(
    @SerialName("custom_name")
    override val name: String, 
    @SerialName("original_t")
    override val originalTime: String
) : Project

@Serializable
class OtherProject(
    @SerialName("custom_name")
    override val name: String, 
    @SerialName("original_t")
    override val originalTime: String,
    val other: String
) : Project

Describe the solution you'd like It should be able to use @SerialName on parent fields:

interface Project {
    @SerialName("custom_name")
    val name: String
    @SerialName("original_t")
    val originalTime: Long
}

@Serializable
class OwnedProject(
    override val name: String, 
    override val originalTime: String
) : Project

@Serializable
class OtherProject(
    override val name: String, 
    override val originalTime: String,
    val other: String
) : Project

benkuly avatar Dec 30 '20 11:12 benkuly

I think other Annotations like Transient and Required etc. should be inherited as well

NoZomIBK avatar Feb 03 '23 13:02 NoZomIBK

Related comment: https://github.com/Kotlin/kotlinx.serialization/issues/1950#issuecomment-1362986792

sandwwraith avatar Dec 10 '24 14:12 sandwwraith