Hey folks, hopefully quick question. I’ve been usi...
# serialization
j
Hey folks, hopefully quick question. I’ve been using Kotlinx serialization for a while and recently was trying to write a custom serializer/descriptor for a nested structure. I then realized that I wasn’t going to be able to reference the descriptor from within `buildClassSerialDescriptor`:
Copy code
class SomeSerializer : KSerializer<SomeClass> {
    override val descriptor: SerialDescriptor = buildClassSerialDescriptor("SomeClass") {
        element<Boolean>("flag")
        element("nested", SomeSerializer().descriptor, isOptional = true)
    }
    // ...
}
This leads to a stack overflow since the
init
of
SomeSerializer
creates the
descriptor
which then tries to new up another
SomeSerializer
. That makes sense to me, but I wasn’t sure what the intended way of accomplishing something like this is. I know it’s possible if using one of the plugin generated serializers. Does this mean I might potentially need to implement
SerialDescriptor
myself or is this something that’s already solved for and I’m just not seeing the path forward?
I did just find this. I had a similar thought but wasn’t sure if it was going to be sufficient or not. I’ll try this out, but it’s a little unfortunate to have to replicate something like this. I would be open to discuss this a bit more too. https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/json/commonMain/src/kotlinx/serialization/json/JsonElementSerializers.kt#L218