I’m getting json data from server API to Android app (Kotlin Serialization + Retrofit)
I need to parse a Json with field errors
. The issue is the structure of this field.
If there are no errors I get empty array:
"errors":[]
But if there are some errors, I get this structure, like a Map with dynamic key:
"errors":{"kode":"The kode field do not exist."}
The question:
What type should I use for errors
field in my Serializable Kotlin data class to be able parse it in both cases?
If I use Map<String, String>
, it works only if there are some errors. For json with empty errors I get exception:
Unexpected JSON token at offset 86: Expected start of the object '{', but had '[' instead at path: $.errors
I tried to use KSerializer to return empty Map if cannot deserialize errors
as a Map
class TestDeserializer: KSerializer<Map<String, String>> {
private val delegateMapSerializer = MapSerializer(String.serializer(), String.serializer())
override val descriptor: SerialDescriptor
get() = SerialDescriptor("Errors", delegateMapSerializer.descriptor)
override fun deserialize(decoder: Decoder): Map<String, String> {
return try {
decoder.decodeSerializableValue(delegateMapSerializer)
} catch (e: Exception) {
emptyMap()
}
}
override fun serialize(encoder: Encoder, value: Map<String, String>) {
TODO("Not yet implemented")
}
}
And use it in my Kotlin data class:
@Serializable(with = TestDeserializer::class)
val errors: Map<String, String>
But probably did smth wrong, because still get the same JsonDecodingException
2
Answers
Found another way to create Deserializer that checks if json element is an array and replaces it with empty map.
Use it for
errors
in data classIf the JSON field is either JSONArray or JSONObject, You can use
JsonElement
.And you can get
errors
like this.parseErrors
Kotlin extension