martmists
05/19/2025, 6:08 PMclass UuidSerializer : KSerializer<UUID> but I want "" to be decoded as null rather than a UUID value.ephemient
05/19/2025, 7:02 PMhfhbd
05/19/2025, 7:03 PMobject MyEmptyUuidSerializer : KSerializer<Uuid?> {
override fun deserialize(decoder: Decoder): Uuid? {
val uuidString = decoder.decodeString()
return if (uuidString.isEmpty()) {
null
} else {
Uuid.parse(uuidString)
}
}
}hfhbd
05/19/2025, 7:04 PMmartmists
05/19/2025, 7:08 PMmartmists
05/19/2025, 7:10 PMType '@Serializable(...) UUID' is non-nullable and therefore can not be serialized with serializer for nullable type 'UuidSerializer'martmists
05/19/2025, 7:12 PMclass StringAsNullUuidSerializer : JsonTransformingSerializer<UUID>(UuidSerializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement {
if (element is JsonPrimitive && element.jsonPrimitive.contentOrNull == "") return JsonNull
return element
}
}
but that errors at runtime with
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Expected string value for a non-null key 'primitive', got null literal instead at element: $.primitivehfhbd
05/19/2025, 7:20 PMmartmists
05/20/2025, 9:01 AMhfhbd
05/20/2025, 9:04 AMUuid?hfhbd
05/20/2025, 9:05 AMmartmists
05/20/2025, 12:35 PMUUID and a duplicate serializer with null support for UUID? ? That seems kinda convoluted