As Serialization work with annotation @Serializable
How do you defined a method that should receive Serializable class or a generic constraints ? with ?

fun (x : ) { ... }

class<T :> { ... }


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?

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) { }

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.

1 Like

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

fun f(@serializable o: Op)

@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 for jvm targets
  • declare abstract streaming virtual operators (for instance) for LLVM targets
  • resolve to nop on JS

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:

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

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

doSomeSerializing(MyDataClass.serialize(), myData)
1 Like

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

just use expect interface Serializable and then in the jvm implementation use a typealias to is an annotation, not an interface. Kotlin cannot force a class to be annotated with a certain annotation at compile time.

@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.

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