I'm trying to deserialize a JSON file to a Kotlin data class I cannot control using kotlinx.serialization
.
The class looks something along the lines of:
public data class Lesson(
val uid: String,
val start: Instant,
val end: Instant,
val module: String,
val lecturers: List<String>,
val room: String?,
val type: String?,
val note: String?
)
The JSON I try to parse looks like this:
{
"lessons": [
{
"uid": "sked.de956040",
"start": "2020-11-02T13:30:00Z",
"end": "2020-11-02T16:45:00Z",
"module": "IT2101-Labor SWE I: Gruppe 1 + 2",
"lecturers": [
"Kretzmer"
],
"room": "-",
"type": "La",
"note": "Prüfung Online"
}
]
}
This is tried via:
@Serializable
data class ExpectedLessons(
val lessons: List<Lesson>
)
val decoded = Json.decodeFromString<ExpectedLessons>(text)
As the class
Lesson
cannot be modified, one cannot add a@Serializable
annotation to make (de)serialization work. So you may create two custom serializers to make it work.You may configure the serializers before using them like this: