I want to serialize BigInteger as a class but, I fail to get some none primitive value: here's my co...
a
I want to serialize BigInteger as a class but, I fail to get some none primitive value: here's my code:
Copy code
object BigIntegerSerializerForMeta :
    KSerializer<BigInteger> {

    override val descriptor: SerialDescriptor = buildClassSerialDescriptor("BigIntSerializer") {
        element("value", PolymorphicPrimitiveSerializer(String.serializer()).descriptor)
    }
    const val radix = 16
    override fun deserialize(decoder: Decoder): BigInteger {
        return BigInteger(decoder.decodeSerializableValue(String.serializer()), radix)

    }
    override fun serialize(encoder: Encoder, value: BigInteger) =
        encoder.encodeSerializableValue(String.serializer(), value.toString(radix))
}
I get {"1234567890"} instead of {"type":BigIntSerializer,"value":"1234567890"}
d
You descriptor doesn't match what you describe. You want a
type
and
value
field right? You only have
value
.
a
when you serialize a class object, the serializer adds the classDiscriminator (defaulted as "type") automatically. so I can just hardcode the type, but how to do it properly? Isn't the serialName of the serialDescriptor should be added to the json?
d
Oh, you want polymorphism. What the base class look like?
a
The base is Any 🙂 , but it will work if I will add just the type in the json
d
I don't understand what you're trying to do but looks like you figured something out.
a
ok, yes, I've solved it by wrapping with encodeStructure/decodeStructure... I don't have so much understanding of what I did, but it works