我有一个类:
@JsonClass(generateAdapter = true)
data class DayAveragePriceModel(
val asset: Asset,
val value: BigDecimal
)
在哪里
Asset
是我的自定义类(class)。我正在尝试使用 Moshi
但我收到以下错误:Caused by: java.lang.IllegalArgumentException: Platform class java.math.BigDecimal (with no annotations) requires explicit JsonAdapter to be registered
我该如何解决这个问题?我试过了
return Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.add(Object::class.java)
.build()
}
但它正在崩溃。
提前非常感谢!
最佳答案
正如异常(exception)所说,这是一种平台类型,您需要使用其公共(public) API 对其进行编码和解码。
object BigDecimalAdapter {
@FromJson fun fromJson(string: String) = BigDecimal(string)
@ToJson fun toJson(value: BigDecimal) = value.toString()
}
return Moshi.Builder()
.add(BigDecimalAdapter)
.add(KotlinJsonAdapterFactory())
.build()
关于android - Moshi:平台类 BigDecimal 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54957945/