i have a test in which i feed `"{"type": "A", ...}...
# serialization
n
i have a test in which i feed
"{"type": "A", ...}"
into a
PolymorphicType.A.serializer()
it fails with a
Unexpected JSON token at offset 11: Encountered an unknown key 'type'.
is there a way to skip that.. while also not adding
type
as a property to the class (because that creates other issue) or using
ignoreUnknownKeys = true
because there could be other.. issues ? oone way i found was to use
Copy code
val a = Json.decodeFromString(PolymorphicType.serializer(), jsonString) as PolymorphicType.A
but that means that there is no way to use this.. except for encoding and decoding using the polymorphic types.. or you wil lose the
type
properties
e
Try
PolymorphicType.serializer()
instead
Or use
ignoreUnknownKeys = true
for your test
d
You don't need the
type
if you know the specific base class. So
type
will be an unknown key. Sadly, kotlinx.serialization can't really be smart about this because a class could be implementing multiple interfaces, each with their own discriminator.
n
this is for doing geojson in which the standard specifies that there should be a
"type"
key present i guess i forgot to mention that requirement
is there a way to delegate kotlinx-serialization to use
override val type = "Point"
and not conflict with it ?
d
Sadly no. I've tried to get around it before. You just have to use the base class.
n
the tricky part is .. because of restrictions we have to make sure that jackson can also still serialize the damn thing.. (gladly no deserialization required) i would be happy if we could jsut drop that whole stack ..
so for now.. i use
@SerialName
and a
@Transient val type: String
to make it somehow work