I try to use kotlin.serialization (1.0.0) with kto...
# serialization
a
I try to use kotlin.serialization (1.0.0) with ktor (1.4.1) and kotlin (1.4.10) I have too classes:
Copy code
@Serializable
class Holder<T> (val value: T)

@Serializable
class StringHolderDirect (val value: String)
The both work with Json directly
Copy code
Json.encodeToString(StringHolderDirect(""))
    Json.encodeToString(Holder(""))}
but with ktor works only the second, the first produces an error:
Copy code
val client = HttpClient(CIO) {
        this.install(JsonFeature) { serializer = KotlinxSerializer() }
    }
    <http://client.post|client.post><String>(URL) {
        contentType(ContentType.Application.Json)
        body = Holder("")
    }
On run:
Copy code
Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'Holder' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
        at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
        at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:130)
        at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
        at io.ktor.client.features.json.serializer.KotlinxSerializerKt.buildSerializer(KotlinxSerializer.kt:77)
What do I wrong?
j
I am going to guess because you try to make a serializable out of a generic class
How would serialization know about the type you are trying to serialize? It could be any type (also something that is not serializable)
a
Shall I define the type for the serializer?
r
or you can pass the specific serializer
json.decodeFromString(serializer, bodyResponse)
a
I get rid of
decodeFromString
😄
s
a
Yes, it is. Thank you!