# Serialization

**URL:** <https://discuss.kotlinlang.org/t/serialization/20209>\
**Category:** Support\
**Created:** [December 10, 2020, 4:13pm UTC](https://discuss.kotlinlang.org/t/serialization/20209 "2020-12-10T16:13:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophels](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@christophels](https://discuss.kotlinlang.org/u/christophels)\
**Post date:** [December 10, 2020, 4:13pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/1 "2020-12-10T16:13:33Z")

</div>

As [Serialization](https://kotlinlang.org/docs/reference/serialization.html) work with annotation @Serializable  
How do you defined a method that should receive Serializable class or a generic constraints ? with java.io.Serializable ?

```
fun (x : java.io.Serializable ) { ... }

class<T : java.io.Serializable> { ... }

```

??

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [December 10, 2020, 4:29pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/2 "2020-12-10T16:29:47Z")

</div>

I’d use `Any`. And put a comment stating that the object should be serializable.

By the way what’s your use-case?  
Being annotated with `@Serializable` doesn’t mean much for a class. It mean that you can try to serialize its instances (but possibly fail), is there a reason you can’t take the object after it has been serialized?

---

<div class="post-metadata">

**Author:** ![christophels](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@christophels](https://discuss.kotlinlang.org/u/christophels)\
**Post date:** [December 11, 2020, 8:40am UTC](https://discuss.kotlinlang.org/t/serialization/20209/3 "2020-12-11T08:40:18Z")

</div>

Wanted to define a generic class that can be serialized/deserialized :

May be with a specific interface, something like ?

```
interface Serializable<T> {
    fun from(in : InputStream) : T
    fun to(out : OutputStream, x : T)
}

class Op<T : Serializable<T>>(val elem : T) { }

```

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [December 11, 2020, 8:47am UTC](https://discuss.kotlinlang.org/t/serialization/20209/4 "2020-12-11T08:47:51Z")

</div>

Java serialization does not have anything to do with kotlin serialization. They are completely orthogonal and work in different ways. Kotlin serialization works on compile-time serializer generation so there is no special interface for it.

---

<div class="post-metadata">

**Author:** ![bjonnh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bjonnh/32/8106_2.png) [@bjonnh](https://discuss.kotlinlang.org/u/bjonnh)\
**Post date:** [December 13, 2020, 2:10am UTC](https://discuss.kotlinlang.org/t/serialization/20209/5 "2020-12-13T02:10:43Z")

</div>

Could this be added as an annotation in kotlinx-serialization?

```auto
fun f(@serializable o: Op)

```

---

<div class="post-metadata">

**Author:** ![claude.brisson](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@claude.brisson](https://discuss.kotlinlang.org/u/claude.brisson)\
**Post date:** [December 15, 2020, 6:00pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/6 "2020-12-15T18:00:17Z")

</div>

`@Serializable` is a good idea, but I don’t think that it’s productive to link it to kotlinx-serialization. You would better have it a standard core annotation that would, at compile-time:

- make (or require that) classes and interfaces implement java.io.Serializable for jvm targets
- declare abstract streaming virtual operators (for instance) for LLVM targets
- resolve to `nop` on JS

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [December 16, 2020, 5:33am UTC](https://discuss.kotlinlang.org/t/serialization/20209/7 "2020-12-16T05:33:33Z")

</div>

If you want to ensure at compile time that the value is serializable with Kotlin Serialization, then you have to explicitly take the serialization strategy.

Notice the two overloads of `Json.encodeToString`. One will only tell you at runtime if the class is not serializable because it dynamically looks for the serializer (and might fail). The other takes the serializer explicitly so there’s no way for it to be missing at runtime.

You’ll need to model you method signatures like this to ensure compile time checking:

```auto
fun <T> doSomeSerializing(serializer: SerializationStrategy<T>, value: T)

```

And you’ll need to provide the serializer when calling:

```auto
doSomeSerializing(MyDataClass.serialize(), myData)

```

---

<div class="post-metadata">

**Author:** ![claude.brisson](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@claude.brisson](https://discuss.kotlinlang.org/u/claude.brisson)\
**Post date:** [December 16, 2020, 12:04pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/8 "2020-12-16T12:04:15Z")

</div>

That’s instructive. But we may not be speaking about the same thing. I’m searching for a way to express in multiplatform kotlin that when targeting a jvm platform, a class/interface will implement or is required to implement java.io.Serializable.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [December 16, 2020, 12:24pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/9 "2020-12-16T12:24:22Z")

</div>

just use `expect interface Serializable` and then in the jvm implementation use a typealias to `java.io.Serializable`.

---

<div class="post-metadata">

**Author:** ![chipays](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@chipays](https://discuss.kotlinlang.org/u/chipays)\
**Post date:** [December 16, 2020, 12:37pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/10 "2020-12-16T12:37:45Z")

</div>

`java.io.Serializable` is an annotation, not an interface. Kotlin cannot force a class to be annotated with a certain annotation at compile time.

---

<div class="post-metadata">

**Author:** ![claude.brisson](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@claude.brisson](https://discuss.kotlinlang.org/u/claude.brisson)\
**Post date:** [December 16, 2020, 12:54pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/11 "2020-12-16T12:54:42Z")

</div>

@Wasabi375 Yes of course, that’s what I meant. My point was to make it part of the core, and maybe add other behaviors for other platforms. kotlinx-serialization is a much higher level library (and could make use of it, detecting right away more non serialization issues at compile time).

@chipays You are [obviously wrong](https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html).

---

<div class="post-metadata">

**Author:** ![chipays](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@chipays](https://discuss.kotlinlang.org/u/chipays)\
**Post date:** [December 16, 2020, 1:01pm UTC](https://discuss.kotlinlang.org/t/serialization/20209/12 "2020-12-16T13:01:14Z")

</div>

Right, The OP has got me confused. Java’s serializable is an interface, Kotlinx’s is an annotation.
