Hello all :wave: I'm currently trying to serialize...
# serialization
m
Hello all 👋 I'm currently trying to serialize a field of type
arrow.core.Option<javax.measure.Unit<T: javax.measure.Quantity<T>>>
(Apidocs: Option, Unit). For the
javax.measure.Unit
type I think that I don't need the type parameter for serialization as I use the UnitFormat interface for my custom serializer. But I think that this is a hard requirement of the serializers module? Or is there a way to register serializers for star projections? Arrow provides a serializer for
Option
in OptionSerializer which is the one I'm using for the outer serializer. My current implementation looks like this:
Copy code
class UnitSerializer<T: Quantity<T>> (foo: KSerializer<T>): KSerializer<Unit<T>> {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("MeasureUnit", PrimitiveKind.STRING)
    override fun deserialize(decoder: Decoder): Unit<T> {
        val string = decoder.decodeString()
        return SimpleUnitFormat.getInstance().parse(string) as Unit<T>
    }

    override fun serialize(encoder: Encoder, value: Unit<T>) {
        val string = SimpleUnitFormat.getInstance().format(value)
        encoder.encodeString(string)
    }
}
I previously tried
KSerializer<Unit<*>>
as the type declaration and when that didn't work I added the information about the type arguments. However during compliation there's a recursion in
Copy code
at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkType(SerializationPluginDeclarationChecker.kt:513)
	at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkTypeArguments(SerializationPluginDeclarationChecker.kt:473)
which ultimately results in a stack overflow. For completeness, this is the class that should be serialized:
Copy code
@file:UseSerializers(
    OptionSerializer::class,
    UnitSerializer::class
)
/* snip */

typealias MeasureUnit<T> = javax.measure.Unit<T>

@Serializable
data class DoubleValue(
    val value: Double,
    val unit: Option<MeasureUnit<*>>
) : DataValue()
I also tried adding
@Contextual
but that didn't make any difference either. I'll probably try narrowing down the problem by leaving out the surrounding
Option
for now and see whether I can get it to work. But any pointers would be welcome.
After some additional googling, I found https://github.com/Kotlin/kotlinx.serialization/issues/1728 - so it's probably not because I'm doing something wrong per se but the recursive type definition of
javax.measure.Unit<Q: Quantity<Q>>
that is the problem. Gonna try writing a class serializer for
DoubleValue
instead.