With the project in place, it is time to demonstrate the ease of serializing and deserializing to and from JSON.
Let’s start with a data class holding student exam results. As you can see in the following snippet, the class contains the students' first name, last name, the exam score, and a penalties field representing a handicap for the final score:
@Serializable
data class Student(val firstName: String,
val lastName: String,
val score: Int,
@Optional val penalties: Int = 0)
To get the serializer and deserialization for the class, all that is required is the use of the Serializable annotation. You might have noticed the Optional annotation on the penalties field. This annotation lets the serialization compiler plugin know the field is optional. Serializing a Student instance...