When annotating your data classes or classes with Serializable, you need to pay attention to what is possible and what is not possible. All the val and var class constructors are supported. However, a class constructor cannot have parameters. For example, the following code will yield a compilation error, since the b parameter is present:
@Serializable
class Data(val a: Int, b: Int)
Serializing classes handle the visibility of the properties regardless of the level (private, protected, and so on). In the next code snippet, the b property has been set as private:
@Serializable
class Data(val a: Int) {
private val b: String = "42"
}
There’s a catch when using serialization with property initializers and setters. Consider an adapted version of the previous Data class:
@Serializable
data class Data(val a: String) {
val b: String = compute()
private fun compute...