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
Dominaezzz
10/30/2022, 10:20 PM
You descriptor doesn't match what you describe. You want a
type
and
value
field right? You only have
value
.
a
Assaf Shouval
10/31/2022, 7:31 AM
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
Dominaezzz
10/31/2022, 3:06 PM
Oh, you want polymorphism. What the base class look like?
a
Assaf Shouval
10/31/2022, 3:13 PM
The base is Any 🙂 , but it will work if I will add just the type in the json
d
Dominaezzz
10/31/2022, 3:59 PM
I don't understand what you're trying to do but looks like you figured something out.
a
Assaf Shouval
10/31/2022, 4:03 PM
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