【发布时间】:2020-09-22 18:48:18
【问题描述】:
我正在使用 Moshi,我有以下数据类
@JsonClass(generateAdapter = true)
data class A (
@Json(name = "_id")
val id: String?,
@Json(name = "b")
val b: B? = null
)
@JsonClass(generateAdapter = true)
data class B (
@Json(name = "_id")
val id: String?,
@Json(name = "foo")
val foo: String? = null
@Json(name = "c")
val c: C? = null
)
@JsonClass(generateAdapter = true)
data class C (
@Json(name = "_id")
val id: String?,
@Json(name = "bar")
val bar: String? = null
)
我的 API 有时将对象仅作为 ID 返回,而其他时候将其作为实际对象返回。例如,有时当我获取对象 A 时,它会返回
{
_id: "111111111",
b: {
_id: "222222222",
foo: "foo",
c: {
_id: "333333333",
bar: "bar"
}
}
}
但其他时候它可能会返回
{
_id: "111111111",
b: "222222222"
}
或
{
_id: "111111111",
b: {
_id: "222222222",
foo: "foo",
c: "333333333"
}
}
如我们所见,它可能会返回一个表示对象的字符串,或填充的对象本身。如何创建自定义 Moshi 适配器来处理这个问题?如果它返回一个表示对象的 id,我希望它创建一个仅填充 id 并将其余字段设置为 null 的对象。
我尝试像这样创建一个自定义适配器
class bAdapter {
@FromJson
fun fromJson(b: Any): B {
return when (b) {
is String -> B(b)
else -> b as B
}
}
}
但我得到了错误
com.squareup.moshi.JsonDataException: java.lang.ClassCastException: com.squareup.moshi.LinkedHashTreeMap cannot be cast to com.example.B
【问题讨论】:
标签: android json kotlin retrofit moshi