You are currently viewing I’m stuck with understanding kotlinx-serialization

I’m stuck with understanding kotlinx-serialization

Currently, I am trying to implement a multiplatform custom format for KXS. All the actual encoding / decoding work is already done, the only thing left is to integrate everything with kotlinx-serialization. And that’s where I am stuck for days now. I just don’t understand how to implement Encoder to encode a named-value format.

I tried everything to better understand how those interfaces work. I looked at all the official formats (JSON, HOCON, CBOR, Properties) and at a lot of third-party format implementations, like YAML or BSON (which is probably the best reference because it is closest to the format I am implementing).

Still, it seems opaque to me. The official implementations use NamedValueEncoder, which is an internal class not meant to be used from the outside. The third-party solutions mostly use AbstractEncoder and mostly just encode the values without the names in Encoder. Which makes no sense taken out of context. I suppose there has to be some point at which these values get their corresponding names, but I don’t know where that happens.

I’m also struggling to understand how the Encoder / CompositeEncoder interaction plays out. And encodeSerializableValue() / encodeSerializableElement() also seems weird to me. For some reason, this is the only point where a name for the value is retrieved (and, at some point encoded) in most of the implementations so far.

I think what would really help me is if someone could walk me through the exact routine that gets executed when you serialize and encode a basic data class, with a few levels of nesting.

But really I’d be glad for any kind of help, since I am stuck here for days now and I don’t really know what to do anymore.

So yeah, thanks in advance!

submitted by /u/Raph0007
[link] [comments]