'Kotlin - Serialization and data class with `is` prefixed methods

I have a data class representing a role of a user.

data class UserRole(
    val entity: Entity,                            
    val role: Role,
    val entityId: String
) {

    fun isAdmin() = role == Role.ADMIN
}

When I do serialize this, I see in addition to entity, role and entityId, I do also get isAdmin in my JSON. How can I stop this?

FYI - This is a KMM class present in commonMain and I do use Firebase for storing it in DB.



Solution 1:[1]

If you don't want to serialize/deserialize a specific property, annotate it with @Exclude. So:

data class UserRole(
    val entity: Entity,                            
    val role: Role,
    val entityId: String
) {
    @Exclude
    fun isAdmin() = role == Role.ADMIN
}

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen