how do I deserialize a polymorphic JSON object whe...
# serialization
b
how do I deserialize a polymorphic JSON object where one object in the hierarchy has no type attribute and requires a different strategy? I've tried a bunch of strategies and it looks like none of the approaches presented in the docs match
Looks like this is the only way to do it:
Copy code
class TypeDefinitionSerializer : JsonContentPolymorphicSerializer<TypeDefinition>(TypeDefinition::class) {
    override fun selectDeserializer(element: JsonElement): DeserializationStrategy<TypeDefinition> {
        return when {
            $$"$ref" in element.jsonObject -> Reference.serializer()
            else -> {
                val type = element.jsonObject["type"]?.jsonPrimitive?.content
                when (type) {
                    "string" -> StringDefinition.serializer()
                    "integer" -> IntegerDefinition.serializer()
                    else -> ObjectDefinition.serializer()
                }
            }
        }
    }
}