背景
所在公司项目中,涉及金额数字,所以在反序列化数字金额时,选择了BigDecimal作为金额相关JSON字段的对象类型。在项目中使用Kotlin语言,搭配Retrofit框架和Moshi作为序列化工具,但Moshi在使用当中发现,是不支持string、number类型的JSON字段转换成BigDecimal类型的参数的。于是需要自定义一个JsonAdapter.Factory来解决这一问题。
先来介绍为什么选BigDecimal
1. 精确的十进制表示
BigDecimal内部,通过整数+小数位存储数据,无精度损失,例如 321.45 存储为 32145 X 10^-2。
2. 完全控制舍入行为
提供多种舍入模式,如RoundingModel.HALF_UP 四舍五入,符合财务规范。
3. 支持任意精度
不受固定自己安长度限制,可处理超大金额或超高精度
4. 避免隐式转换风险
强制使用显示计算方式 如,add()
、mulitply()
,避免意外错误。
代码如下
kotlin
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import java.lang.reflect.Type
import java.math.BigDecimal
class BigDecimalAdapterFactory : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<Annotation?>, moshi: Moshi): JsonAdapter<*>? {
if (type != BigDecimal::class.java) return null
return object : JsonAdapter<BigDecimal>() {
override fun fromJson(reader: JsonReader): BigDecimal? {
return when (reader.peek()) {
JsonReader.Token.NUMBER -> {
val number = reader.nextDouble()
BigDecimal.valueOf(number)
}
JsonReader.Token.STRING -> {
val string = reader.nextString()
BigDecimal(string)
}
JsonReader.Token.NULL -> {
null
}
else -> {
val intValue = reader.nextInt()
BigDecimal(intValue)
}
}
}
override fun toJson(wirtter: JsonWriter, value: BigDecimal?) {
if (value != null) {
wirtter.value(value)
} else {
wirtter.nullValue()
}
}
}.nullSafe()
}
}