【发布时间】:2019-12-14 20:46:42
【问题描述】:
我正在尝试在moshi 中编写自定义MapAdapter 我的要求是忽略地图中的任何不良元素。我已经成功编写了反序列化方法(fromJson()),但是我遇到了toJson 的麻烦。这是我的toJson() 方法。
override fun toJson(writer: JsonWriter, value: Map<Any?, Any?>?) {
if (value == null) {
writer.nullValue()
} else {
writer.beginObject()
value.forEach {
writer.name(elementKeyAdapter.toJsonValue(it.key).toString())
.value(elementValueAdapter.toJson(it.value))
}
writer.endObject()
}
}
这段代码的问题是它总是在最终的 Json 中将 map 中的值作为字符串写入。例如考虑这段代码
enum class VehicleType2 {
@Json(name ="type1")
TYPE1,
@Json(name ="type2")
TYPE2,
@Json(name ="type3")
TYPE3,
@Json(name ="type4")
TYPE4,
}
和
val map = mutableMapOf<VehicleType2, Int>()
map[VehicleType2.TYPE1] = 1
map[VehicleType2.TYPE2] = 2
val adapter: JsonAdapter<Map<VehicleType2, Int>> =
moshi.adapter(Types.newParameterizedType(Map::class.java, VehicleType2::class.java, Integer::class.java))
Log.i("test", adapter.toJson(map))
这会导致跟随 Json
{"type1":"1","type2":"2"}
注意1 和2 是strings 而不是integers。到目前为止,我尝试了很多排列都没有成功。
这里是more complete sample,它重现了这个问题。
【问题讨论】:
标签: serialization kotlin android-json jsonserializer moshi