Daniel Perez
01/26/2023, 5:37 PMSerializer to handle a JsonObject ? I would like to achieve String -> JsonObject -> Logic with when statement -> *MyChosenClass* . Thank you.jw
01/26/2023, 5:39 PMMyChosenClass? or JsonObject?
It sounds like you're describing a normal delegating KSerializer<MyChosenClass> where the delegate is JsonObject.serializer()Daniel Perez
01/26/2023, 5:41 PMMyChosenClass is the property I'm trying to get to after evaluating a typeId from its wrapper class. So
@Serializable(WrapperSerializer::class)
data class Wrapper(
val typeId: Long,
val myChosenClass: MyChosenClass,
)
The logic I mentioned is occurring inside the WrapperSerializer. I can get the JsonObject just fine, it's just after I've identified which serializer to use, I'm not sure how to apply it to a JsonObjectAlex Rouse
01/26/2023, 5:42 PMJsonContentPolymorphicSerializer may be what you are looking for https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-content-polymorphic-serializer/Daniel Perez
01/26/2023, 5:49 PMwhen(typeId) {
1 -> Wrapper(typeId = typeId, myChosenClass = decoder.decode(MyChosenClass.Subclass1.serializer(jsonObject)))
}jw
01/26/2023, 5:51 PMDecoder because you're not reading from the original JSON but from a model objectjw
01/26/2023, 5:51 PMjson.decodeFromJsonElement(serializer, jsonObject)jw
01/26/2023, 5:52 PMjson format as the source, use the chosen serializer of the chosen subclass, and use jsonObject as the model from which to bind properties. There's no actual serialized JSON decoding happening here.Daniel Perez
01/26/2023, 6:03 PMjsonObject into my chosen subclass though? I was trying to avoid that since there's quite a few subclasses and I just wanted to select the serializer from the typeId .jw
01/26/2023, 6:04 PMDaniel Perez
01/26/2023, 6:12 PM