are there any examples of a custom serializer that...
# serialization
j
are there any examples of a custom serializer that unwraps an array of multiple elements which does not use
JsonTransformingSerializer
? I want to unpack a two-element array to a single type but all of the examples seem to be around objects with keys or simple scalar transformation.
e
I threw together something extremely hacky here.. It's almost working, perhaps it's enough to get started. 🙂 https://github.com/Kantis/ks3/commit/0b548111334e7da0893f3d6cd61a9002ee83aab7
d
^ That is indeed more/less how to do it. The descriptor is bit overkill for JSON but should work.
A simpler option might be to write a serializer that uses
ListSerializer()
under the hood. It's not using
JsonTransformingSerializer
but I have a feeling you don't want to do it that way either 😛.
j
Hmm true. Maybe I'll do that so I can just do a quick unpacking.
Copy code
private object TwoStringArrayIntRangeSerializer : KSerializer<IntRange> {
	private val delegate = ListSerializer(String.serializer())
	override val descriptor get() = delegate.descriptor
	override fun deserialize(decoder: Decoder): IntRange {
		val (start, end) = delegate.deserialize(decoder)
		return start.toInt()..end.toInt()
	}
welp i feel dumb for not doing this right away. so much better!