Hey folks, is it possible to make a custom (de-) serializer (`KSerializer`) configurable? Specifical...
j
Hey folks, is it possible to make a custom (de-) serializer (
KSerializer
) configurable? Specifically I'm looking for a way to make a serializer for
BigDecimal
configurable regarding the scale of the
BigDecimal
. Contextual serialization doesn't seem to be a fit because it would only work per class and not per field, right?
A "naive" serializer such as the one described in https://woohuiren.me/blog/custom-kotlin-serializer-for-bigdecimal/ doesn't work because the scale is fixed (default).
Is something like this the best we can do?
Copy code
abstract class ScalableBigDecimalSerializer(private val scale: Int): KSerializer<BigDecimal> { ... }

object Scale2BigDecimalSerializer: ScalableBigDecimalSerializer(2)
y
Personally I use such a variant bcz I really haven't a lot of serializers (just to of them), but you can try to go with this: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serial-info/
j
Thanks, Alex. I'll look into it. 🙂