Skip to content Skip to sidebar Skip to footer

Kotlin Serialization: Serializer Has Not Been Found For Type 'uuid'

I am using Kotlin Serialization to serialize a custom type which contains a UUID field @Serializable data class MyDataClass { val field1: String, val field2: UUID } I got

Solution 1:

After following the Kotlin Custom Serializer section of the Kotlin Serialization Guide, I realized I had to write an object that looks like this to actually help the UUID serialize/ deserialize, even though UUID already implements java.io.Serializable:

object UUIDSerializer : KSerializer<UUID> {
        overrideval descriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING)

        overridefundeserialize(decoder: Decoder): UUID {
                return UUID.fromString(decoder.decodeString())
        }

        overridefunserialize(encoder: Encoder, value: UUID) {
                encoder.encodeString(value.toString())
        }
}

// And also update the original data class:@SerializabledataclassFaceIdentifier(
        val deviceId: String,
        @Serializable(with = UUIDSerializer::class)val imageUUID: UUID,
        val faceIndex: Int
)

Well it turns out I have to do this for a lot of types: e.g. Rect, Uri, so I will be using Java serialization if possible... Let me know if you know a simpler way.

Post a Comment for "Kotlin Serialization: Serializer Has Not Been Found For Type 'uuid'"