https://kotlinlang.org logo
#serialization
Title
# serialization
v

Vampire

07/11/2022, 2:02 AM
I want to port the custom serializer at https://github.com/Vampire/setup-wsl/blob/master/buildSrc/src/main/kotlin/net/kautler/dao/GitHubAction.kt#L73 that still runs on 0.20.0 to 1.3.x. I there have used
override val descriptor = SerialDescriptor("...", CONTEXTUAL)
for the
KSerializer
. How would I port that to 1.3.x as the function does not expect a kind anymore?
s

Sebastian Schuberth

07/11/2022, 6:47 AM
Would using the (experimental) `@Serializer`annotation maybe work?
Ah, sorry, just saw you're actually already using it at https://github.com/Vampire/setup-wsl/blob/bcd134a8857d9afc02db9cc3fa981a30e231ab61/buildSrc/src/main/kotlin/net/kautler/dao/GitHubAction.kt#L67. I thought specifying the
descriptor
is then not required anymore.
v

Vampire

07/11/2022, 5:09 PM
Hm, not at my box right now, but I think if I remove the descriptor it complains that it is missing and if I make the class abstract it cannot instantiate it.
v

Vampire

07/11/2022, 7:14 PM
Your example does not match, because it is not about a generic type. For a generic type like in my case you do not need an
object
, but a class with constructor and also a provided descriptor. And additionally the serializer is contextual. I needed quite some time and comments to get it working properly back then with 0.20.0.
Hm, I found that it works if I use
Copy code
@InternalSerializationApi
override val descriptor = buildSerialDescriptor("...", CONTEXTUAL)
but I don't like the internal serialization api being used. It's documentation says that it should not be used, and if necessary issues filed. So is there another way to get a serial descriptor with
CONTEXTUAL
kind?
e

Emil Kantis

08/01/2022, 8:58 PM
Wouldn't a Map serial descriptor be appropriate in this case?
Copy code
mapSerialDescriptor<String, Output>()
v

Vampire

08/01/2022, 9:18 PM
No, it must be a contextual descriptor or the processes is not done contextually
3 Views