GlenPeterson
GlenPeterson

Reputation: 5206

How do I implement Serializable in Kotlin so it also works in Java?

I have some code that I've been using for years in Java, but need it in Kotlin (its interfaces extend collections interfaces). Some classes are serializable. When I try the obvious, I get "Cannot access 'Serializable': it is internal in kotlin.io":

class Foo(val someField:Int): Serializable {
    companion object {
        private const val serialVersionUID = 20180617104400L
    }
}

So, do I just import java.io.Serializable, or will that cause other issues?

Upvotes: 39

Views: 39145

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170723

do I just import java.io.Serializable

Yes. Just be aware that Kotlin uses @Transient annotation instead of a keyword.

Of course, Java serialization does have its issues, but there's no difference in that respect between Kotlin and Java, and if you are happy with your current code...

Upvotes: 36

Related Questions