vurmanhattan.blogg.se

Kotlin serializable data class
Kotlin serializable data class













kotlin serializable data class

On the JVM, if the generated class needs to have a parameterless constructor, default values for the properties have to be specified (see Constructors). First, make a class serializable by annotating it with Serializable. Providing explicit implementations for the componentN() and copy() functions is not allowed.ĭata classes may extend other classes (see Sealed classes for examples). The API is located in the the rialization package and its format-specific subpackages such as. If the functions of the supertype cannot be overridden due to incompatible signatures or due to their being final, an error is reported. If a supertype has componentN() functions that are open and return compatible types, the corresponding functions are generated for the data class and override those of the supertype. If there are explicit implementations of equals(), hashCode(), or toString() in the data class body or final implementations in a superclass, then these functions are not generated, and the existing implementations are used. The primary constructor needs to have at least one parameter.Īll primary constructor parameters need to be marked as val or var.ĭata classes cannot be abstract, open, sealed, or inner.Īdditionally, the generation of data class members follows these rules with regard to the members' inheritance: rialization reduces the size of the JSON that results when serializing objects by omitting the default values of object properties.

kotlin serializable data class

To ensure consistency and meaningful behavior of the generated code, data classes have to fulfill the following requirements: ToString() of the form "User(name=John, age=42)"ĬomponentN() functions corresponding to the properties in their order of declaration. The compiler automatically derives the following members from all properties declared in the primary constructor: Data class User(val name: String, val age: Int)















Kotlin serializable data class