Kotlinx.serialization: Provide public API to create typed collection serializers

Created on 16 Oct 2017  路  3Comments  路  Source: Kotlin/kotlinx.serialization

There is a need to have top-level serializer extension properties named list, set, and map that construct build-in collection serializers from the element serializers with the following signatures:

val <T> KSerializer<T>.list: KSerializer<List<T>>
val <T> KSerializer<T>.set: KSerializer<Set<T>>
val <K, V> Pair<KSerializer<K>, KSerializer<V>>.map: KSerializer<Map<K, V>>

So, a list of serializable Data items received via REST API in str: String could be deserialized conveniently with:

val list = JSON.parse(str, Data.list) // inferred type of list is List<Data>
feature

Most helpful comment

You can get your List/Set... serializer from the default serializer of class Foo with Foo.class.serializer().list

All 3 comments

Implemented.

The test case shows how to use it with custom serializers, but how can we use it with a list of objects using the default serializer from @Serializable?

You can get your List/Set... serializer from the default serializer of class Foo with Foo.class.serializer().list

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kastork picture kastork  路  3Comments

ersin-ertan picture ersin-ertan  路  3Comments

daweedm picture daweedm  路  4Comments

just-kip picture just-kip  路  3Comments

erikc5000 picture erikc5000  路  3Comments