czuckie
09/02/2023, 12:24 PMczuckie
09/02/2023, 12:25 PM{
"type":"result",
"result": {} | []
}
The result field can contain a single instance of an item, or a list of that particular item.
Is my only option a custom serializer?czuckie
09/02/2023, 1:57 PMresult
field will contain an array or an object and I can't see anyway to interrogate what the next symbol is going to be in the KSerializer
implementation (via the decoder instance)asdf asdf
09/02/2023, 3:31 PMasdf asdf
09/02/2023, 3:37 PM@Serializable
data class Data(
val type: String,
@Serializable(with = OneOrManySerializer::class)
val result: List<Result>
)
class OneOrManySerializer<T>(serializer: KSerializer<T>) :
JsonTransformingSerializer<List<T>>(ListSerializer(serializer)) {
override fun transformDeserialize(element: JsonElement): JsonArray =
if (element is JsonArray) element
else JsonArray(listOf(element))
}
Which will transform the item to always be a listczuckie
09/03/2023, 10:23 AM