Kotlinx.serialization - deserializer runs before init blocks?

The code below prints “Default”, not “abc” as I expected. Apparently the init block runs after deserialization, and wipes out whatever was set by the deserializer. I thought init blocks ran as part of the primary constructor, which would run before anything else could touch the object. Is that not so?

@Serializable
class Foo {
    var name: String
    init {
        name = "Default"
    }
}

fun main() {
    val foo = Foo()
    foo.name = "abc"
    val json = Json.stringify(Foo.serializer(), foo)
    val foo2 = Json.parse(Foo.serializer(), json)
    println(json)
    println(foo2.name)
}

Prints:

{“name”:“abc”}
Default

Use:

@Serializable 
data class Foo(var name: String = "Default")

Consider if you really need var instead of val tho.

Yes, that works, but my example code is a boiled down example from more complex code. I need a var due to other issues with the serialization library and class hierarchies (discussed in a recent post). I may be able to get rid of my init block, but I want to understand what’s going on. Is it the intent of the serialization library to behave like this, or a bug?

Uhm, I think there can be a way to easy things up and avoid the init block at all.
Try post something in here, let’s see what we can do.