Hi. I'd like to use Java's standard serialisation ...
# serialization
g
Hi. I'd like to use Java's standard serialisation (e.g.,
ObjectOutputStream
) with Kotlin data classes, and I tried using this library as follows:
Copy code
@Serializable
internal data class FileSessionPublicKeyData(
    val keyId: ByteArray,
    val keyDer: ByteArray,
    val creationTimeTimestamp: Long
)
But I get
java.io.NotSerializableException: tech.relaycorp.awala.keystores.file.FileSessionPublicKeyData
when I try to save a value to a file using
ObjectOutputStream
, which I guess that
@Selializable
doesn't actually implement Java's
Serializable
interface. Is there any way to do this? I don't want to use JSON, protobuff, etc.
e
kotlinx.serialization.Serializable
has no relation to
java.io.Serializable
if you want to make your object Java serializable, you need to write it like Java,
Copy code
import java.io.Serializable

class FileSessionPublicKeyData(
    ...
) : Serializable {
    companion object {
        // required if you want to allow for serialization across compatible changes
        private const val serialVersionUID = (some constant Long)
    }
}
if you want both kotlinx.serialization and Java serialization, then you need to declare both, e.g.
Copy code
import java.io.Serializable as JavaSerializable
import kotlinx.serialization.Serializable

@Serializable
class FileSessionPublicKeyData(
    ...
) : JavaSerializable
g
Gotcha. Thanks!
p
Unsolicited advice: Don't use java serialization if at all possible
👍 1