Moshi @Json注释不适用于com.github.kittinunf.fuel.moshi.moshiDeserializerOf吗?

时间:2019-05-09 12:35:10

标签: kotlin moshi kotlin-fuel

我有一个ResponseObject:

data class ResponseObject(
        val notCamelcase: String,
        val param2: String,
        val param3: String
)

请注意,响应JSON正文中的第一个参数不是驼峰字母(例如notCamelCase)。

Furtheron,我触发了REST调用with the FUEL library

Fuel.get(someParam)
        .responseObject(moshiDeserializerOf(ResponseObject::class.java)) { _, response, result ->
            try {
                if (response.statusCode == HttpURLConnection.HTTP_OK) {
                    val responseObject = result.component1()
        }

以下是我的进口商品:

import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.core.HttpException
import com.github.kittinunf.fuel.moshi.moshiDeserializerOf

要在我的以下代码中使用驼峰式我修改了ResponseObject:

data class ResponseObject(
        @Json(name="notCamelcase")
        val notCamelCase: String,
        val param2: String,
        val param3: String
)

is described here....

在这种情况下,notCamelCase为空。 @Json是否仅与com.github.kittinunf.fuel.moshi.moshiDeserializerOf不兼容?怎么了?

1 个答案:

答案 0 :(得分:0)

对Moshi kotlin使用@field:Json()注释。

data class ResponseObject(
    @field:Json(name="notCamelcase")
    val notCamelCase: String,
    val param2: String,
    val param3: String
)

参考:https://github.com/square/moshi/issues/315 如讨论中所述,这仍然是一种解决方法。官方的Kotlin支持是正确的选择:https://github.com/square/moshi#kotlin