g

    Gus

    10 months ago
    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:
    @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

    ephemient

    10 months ago
    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,
    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.
    import java.io.Serializable as JavaSerializable
    import kotlinx.serialization.Serializable
    
    @Serializable
    class FileSessionPublicKeyData(
        ...
    ) : JavaSerializable
    g

    Gus

    10 months ago
    Gotcha. Thanks!
    p

    Paul Griffith

    10 months ago
    Unsolicited advice: Don't use java serialization if at all possible