# Sealed class and generic types kotlinx-serialization

**URL:** https://discuss.kotlinlang.org/t/sealed-class-and-generic-types-kotlinx-serialization/29845
**Category:** Support
**Created:** [February 5, 2025, 11:12am UTC](https://discuss.kotlinlang.org/t/sealed-class-and-generic-types-kotlinx-serialization/29845 "2025-02-05T11:12:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vh0s](https://avatars.discourse-cdn.com/v4/letter/v/f0a364/32.png) [@vh0s](https://discuss.kotlinlang.org/u/vh0s)
#### Post date: [February 5, 2025, 11:12am UTC](https://discuss.kotlinlang.org/t/sealed-class-and-generic-types-kotlinx-serialization/29845/1 "2025-02-05T11:12:01Z")

</div>

Hello, I have a problem regarding kotlinx-serialization. I am writing a library under Sockets (java) Request/Response (classes below) and unfortunately when receiving packets I have to deserialize to SocketRespone even though Any is not serialized. Have you perhaps had a similar problem and have a patent on how to do this?

Class Request/Response

```auto
@Serializable
public sealed class SocketResponse<out TYPE> {
    public abstract val id: Uuid

    @Serializable
    public data class Success<out TYPE>(
        public override val id: Uuid,
        public val result: TYPE
    ): SocketResponse<TYPE>()

    @Serializable
    public data class Failure(
        public override val id: Uuid,
        public val message: String
    ): SocketResponse<Nothing>()
}

```

```auto
val line = connection.reader.readLine() ?: return@launch
val request = Json.decodeFromString<SocketRequest<Any>>(line) <---- Error because Any does not have a serializer

```

---

<div class="post-metadata">

### Author: ![mrbenjoi](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@mrbenjoi](https://discuss.kotlinlang.org/u/mrbenjoi)
#### Post date: [February 9, 2025, 6:29am UTC](https://discuss.kotlinlang.org/t/sealed-class-and-generic-types-kotlinx-serialization/29845/2 "2025-02-09T06:29:19Z")

</div>

The class definiton you showed is for `SocketResponse` but the deserialization tries to deserialize to `SocketRequest`.

Can you post the definiton of the latter as well?

---

<div class="post-metadata">

### Author: ![mrbenjoi](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@mrbenjoi](https://discuss.kotlinlang.org/u/mrbenjoi)
#### Post date: [February 9, 2025, 6:51am UTC](https://discuss.kotlinlang.org/t/sealed-class-and-generic-types-kotlinx-serialization/29845/3 "2025-02-09T06:51:31Z")

</div>

I guess the actual problem though is, that kotlinx serialization is not able to simply de/serialize `Any` because it does not use reflection to identify any appropriate type for it.

You either have to use a `@Serializable` type instead of `Any` or implement a custom deserializer for `Any`.

But maybe someone else knows another possibility as well.
