-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contextually register in SerializersModule nullable type #1826
Comments
The problem is in |
Is there a workaround? I'm currently stuck on this, I'm surprised it's not possible to serialize nullable values. |
The same problem here, registering nullable string serializer like this: object NullableStringSerializer : KSerializer<String?> {
private val delegate = serializer<String>().nullable
override val descriptor: SerialDescriptor = delegate.descriptor
override fun serialize(encoder: Encoder, value: String?) {
delegate.serialize(encoder, value?.ifEmpty { null })
}
override fun deserialize(decoder: Decoder): String? {
return delegate.deserialize(decoder)?.ifEmpty { null }
}
}
val module = SerializersModule {
contextual(NullableStringSerializer)
// ~~~~~~~~~~~~~~~~~~~~~~~~
} This results in error:
Is there any workaround available please? |
@morki Maybe |
Registering serializer module as contextual and passing a KSerializer that has nullable type is not allowed. It is needed due to bad backend responses when field value is not null, but empty string instead.
Example:
val module = SerializersModule {
contextual(serializer = LocalDateSerializer) -> shows error Type mismatch. Required: Any Found: LocalDate?
}
LocalDateSerializer class
@Serializer(forClass = LocalDate::class)
object LocalDateSerializer : KSerializer<LocalDate?> {
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDate?", PrimitiveKind.STRING).nullable
}
The text was updated successfully, but these errors were encountered: