admin管理员组文章数量:1313807
I'm having an issue that my field name changes depending on my response from other items. In Kotlin Serialization I know we can serialize fields and such like this
@Serializable data class WeatherResponse (@SerialName("some_weather_id") val weatherData:WeatherData )
However, in my case "some_weather_id" annotation can change based on previous data and api responses. It can be "another_weather_id" etc.
My question is how can we change that or make the annotation dynamic?
I tried implementing a custom serializer, but I get the error
Field 'weatherData' is required for type with serial name 'com.test.WeatherResponse', but it was missing at path: $
Here is my custom serializer in case I'm doing something incorrect
object SerialNameSerializer : KSerializer<String> {
override val descriptor = PrimitiveSerialDescriptor("DynamicSerialName", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: String) {
val dynamicName = getWeatherID()
encoder.encodeStructure(descriptor){
encodeStringElement(descriptor, 0, dynamicName)
encodeStringElement(descriptor,1,value)
}
}
override fun deserialize(decoder: Decoder): String {
val dynamicName = getWeatherID()
decoder.decodeStructure(descriptor){
val name = decodeStringElement(descriptor,0)
if(name != dynamicName) {
throw SerializationException("Unexpected field name: $name")
}
return@decodeStructure decodeStringElement(descriptor, 1)
}
return dynamicName
}
private fun getWeatherID(): String {
return "some_weather_id"
}
}
I'm having an issue that my field name changes depending on my response from other items. In Kotlin Serialization I know we can serialize fields and such like this
@Serializable data class WeatherResponse (@SerialName("some_weather_id") val weatherData:WeatherData )
However, in my case "some_weather_id" annotation can change based on previous data and api responses. It can be "another_weather_id" etc.
My question is how can we change that or make the annotation dynamic?
I tried implementing a custom serializer, but I get the error
Field 'weatherData' is required for type with serial name 'com.test.WeatherResponse', but it was missing at path: $
Here is my custom serializer in case I'm doing something incorrect
object SerialNameSerializer : KSerializer<String> {
override val descriptor = PrimitiveSerialDescriptor("DynamicSerialName", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: String) {
val dynamicName = getWeatherID()
encoder.encodeStructure(descriptor){
encodeStringElement(descriptor, 0, dynamicName)
encodeStringElement(descriptor,1,value)
}
}
override fun deserialize(decoder: Decoder): String {
val dynamicName = getWeatherID()
decoder.decodeStructure(descriptor){
val name = decodeStringElement(descriptor,0)
if(name != dynamicName) {
throw SerializationException("Unexpected field name: $name")
}
return@decodeStructure decodeStringElement(descriptor, 1)
}
return dynamicName
}
private fun getWeatherID(): String {
return "some_weather_id"
}
}
Share
Improve this question
asked Jan 30 at 21:49
OdinsBeardOdinsBeard
395 bronze badges
1 Answer
Reset to default 1If the names are known, you can use JsonNames like:
@Serializable
data class WeatherResponse(
@JsonNames("some_weather_id", "another_weather_id") val weatherData: WeatherData
)
More examples: https://github/Kotlin/kotlinx.serialization/blob/master/docs/json.md#alternative-json-names
本文标签: androidIs there a way to handle dynamic SerialName annotations for Kotlin SerializerStack Overflow
版权声明:本文标题:android - Is there a way to handle dynamic SerialName annotations for Kotlin Serializer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741934819a2405788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论