'Kotlin: How to serialize a interface and ignore the class attributes

interface MyInterface {
    fun schemaName(): String
    fun data(): Any
}

data class Object1(
    val name: String,
    val title: String,
    val description: String,
    val list: List<String>
): MyInterface {
    override fun schemaName(): String = "SCHEMA_FOR_OBJECT1"
    override fun data(): Any = this
}

With this MyInterface and Object1, Kotlin serializes my Object1 into

{
    "name": xxx,
    "title": xxxx,
    "description": xxxx,
    "list": [
        "xxxx",
        "xxxx"
    ]
}

This is understandable but is there anyway I can serialize Object1 into

{
    "schemaName": "SCHEMA_FOR_OBJECT1",
    "data": {
        {
            "name": xxx,
            "title": xxxx,
            "description": xxxx,
            "list": [
                "xxxx",
                "xxxx"
            ]
        }
    }
}

I want to have multiple objects which implement MyInterface but I want them all to serialize into a json with common parent keys.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source