kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Add extension for encoding collections

Open ansman opened this issue 4 years ago • 2 comments

What is your use-case and why do you need this feature?

There is an extension for encoding structures (encodeStructure) but no extension for encoding collection which would be useful for custom serializers.

Describe the solution you'd like I'd like for an extension to be added. It's signature would be:

public inline fun Encoder.encodeCollection(
    descriptor: SerialDescriptor,
    collectionSize: Int,
    block: CompositeEncoder.() -> Unit
)

ansman avatar Nov 01 '21 13:11 ansman

and where is the equivalent decodeCollection or how does one decode a structure encoded with encodeCollection?

Aphexus avatar Dec 08 '24 05:12 Aphexus

@Aphexus encodeCollection is simply the same as beginStructure with a collection size parameter. How you decode it depends on where you've stored this collection size. Generally, Decoder.beginStructure is enough with optional decodeCollectionSize to return the stored size

sandwwraith avatar Dec 09 '24 11:12 sandwwraith