使用任何类型的 HashMap 时,Kotlinx 序列化 @Body 转换器错误
Kotlinx Serialization @Body converter error while using HashMap with Any type
我正在使用 Kotlinx Serialization
和 Retrofit
并尝试通过 HashMap
在 Body
发送参数,如下所示
@POST("post")
suspend fun sendPost(@Body params : HasMap<String, @Contextual @RawValue Any>) : GenericResponse<Post>
但是它给出了这个错误信息
Unable to create @Body converter for java.util.HashMap<java.lang.String, java.lang.Object> (parameter #1)
我也将 SerializersModule
添加到 Retrofit
但它没有帮助
fun provideRetrofit(baseUrl : String, okHttpClient: OkHttpClient) : Retrofit {
val contentType = "application/json".toMediaType()
val json = Json {
serializersModule = SerializersModule {
contextual(String.serializer())
contextual(Int.serializer())
contextual(Double.serializer())
contextual(Boolean.serializer())
}
}
return Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(json.asConverterFactory(contentType))
.build()
}
有人可以帮忙吗?
Kotlinx Version 1.0.1
Kotlinx Converter Version 0.8.0
Kotlin Version 1.4.10
根据 this issue 我们无法做到这一点
我正在使用 Kotlinx Serialization
和 Retrofit
并尝试通过 HashMap
在 Body
发送参数,如下所示
@POST("post")
suspend fun sendPost(@Body params : HasMap<String, @Contextual @RawValue Any>) : GenericResponse<Post>
但是它给出了这个错误信息
Unable to create @Body converter for java.util.HashMap<java.lang.String, java.lang.Object> (parameter #1)
我也将 SerializersModule
添加到 Retrofit
但它没有帮助
fun provideRetrofit(baseUrl : String, okHttpClient: OkHttpClient) : Retrofit {
val contentType = "application/json".toMediaType()
val json = Json {
serializersModule = SerializersModule {
contextual(String.serializer())
contextual(Int.serializer())
contextual(Double.serializer())
contextual(Boolean.serializer())
}
}
return Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(json.asConverterFactory(contentType))
.build()
}
有人可以帮忙吗?
Kotlinx Version 1.0.1
Kotlinx Converter Version 0.8.0
Kotlin Version 1.4.10
根据 this issue 我们无法做到这一点