使用损坏的远程API解析json

时间:2021-06-01 14:50:24

Here is my model class

这是我的模型类。

data class Article( val id: Int? = 0, val is_local: Boolean? = false, val comments: List<Comment?>? = listOf())

数据类文章(val id: Int?= 0, val is_local: Boolean?= false, val comments: List ?=自然()) ?>

and here is json

这里是json

  {
    "id": 33,
    "is_local": "true",
    "comments":
         [
          { "url": "aaa" },

          { "url": "bbb" },

          { "url": "ccc" )

     ]

}

i am using this custom Adapter to return default value in case of parsing error like in my case is is_local field

我正在使用这个自定义适配器来返回在解析错误时的默认值,在我的例子中是is_local字段

class DefaultOnDataMismatchAdapter<T> private constructor(private val delegate: 
   JsonAdapter<T>, private val defaultValue: T?) : JsonAdapter<T>() {

 @Throws(IOException::class)
  override fun fromJson(reader: JsonReader): T? =
        try {
            delegate.fromJsonValue(reader.readJsonValue())
        } catch (e: Exception) {
            println("Wrongful content - could not parse delegate " + 
delegate.toString())
            defaultValue
        }

@Throws(IOException::class)
override fun toJson(writer: JsonWriter, value: T?) {
    delegate.toJson(writer, value)
}

  companion object {
    @JvmStatic
    fun <T> newFactory(type: Class<T>, defaultValue: T?): 
    JsonAdapter.Factory {
        return object : JsonAdapter.Factory {
            override fun create(requestedType: Type, annotations: 
      Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
                if (type != requestedType) {
                    return null
                }
                val delegate = moshi.nextAdapter<T>(this, type, 
          annotations)
                return DefaultOnDataMismatchAdapter(delegate, 
     defaultValue)
             }

         }
      }
   }

}

and my test fail and boolean value is not false i have added the above adapter to moshi

我的测试失败,布尔值不是假的,我已经将上面的适配器添加到moshi中

@Before
fun createService() {

    val moshi = Moshi.Builder()


    .add(DefaultOnDataMismatchAdapter
                           .newFactory(Boolean::class.java,false))
            .add(KotlinJsonAdapterFactory())
            .build()

    val retrofit = Retrofit.Builder()
            .baseUrl(mockWebServer.url("/"))
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .build()

    service =  retrofit.create(ApiStores::class.java)



}




@Test
fun getBooleanParsingError() {

    enqueueResponse(case1)

    val article = service.getArticle().execute()

    assert(article.body()!!).isNotNull()
    assert(article.body()!!.is_local).isEqualTo(false)  // test fail here 

}

but when i change the datatype of is_local field in the model class to not nullable it works

但是当我将model类中的is_local字段的数据类型更改为不可空时,它就可以工作了

1 个解决方案

#1


0  

The issue is that types kotlin.Boolean and kotlin.Boolean? correspond to 2 different Java types:

问题是kotlin的类型。布尔和kotlin.Boolean吗?对应于两种不同的Java类型:

  • kotlin.Boolean is boolean Java primitive type
  • 芬兰湾的科特林。布尔是布尔Java原始类型
  • kotlin.Boolean? is java.lang.Boolean Java type
  • kotlin.Boolean吗?. lang。布尔Java类型

In your test you created an adapter for a kotlin.Boolean (i.e., Java boolean type), while in your data model you have a kotlin.Boolean? (i.e., a java.lang.Boolean type). For this reason, when method create(...) of the Factory gets called, you are in a situation where type != requestedType, so your adapter is not created and the KotlinJsonAdapter is used instead. At this point, since the is_local field of the Json is not a boolean (but a string), Moshi should raise an Exception.

在您的测试中,您为一个kotlin创建了一个适配器。布尔(即。在您的数据模型中,您有一个kotlin.Boolean?(即。. lang。布尔类型)。由于这个原因,当调用工厂的method create(…)时,您处于type != requestedType的情况,因此没有创建适配器,而是使用KotlinJsonAdapter。此时,由于Json的is_local字段不是布尔(而是字符串),因此Moshi应该提出一个异常。

If you change your data model or your adapter to use the same type, you are in a situation where type == requestedType, so your adapter gets created, the exception is thrown as before, but you added a catch block that returns the default value.

如果您更改您的数据模型或适配器以使用相同的类型,那么您将处于type == requestedType的情况,因此您的适配器将被创建,异常将像以前一样抛出,但是您添加了一个catch块,该块返回默认值。

#1


0  

The issue is that types kotlin.Boolean and kotlin.Boolean? correspond to 2 different Java types:

问题是kotlin的类型。布尔和kotlin.Boolean吗?对应于两种不同的Java类型:

  • kotlin.Boolean is boolean Java primitive type
  • 芬兰湾的科特林。布尔是布尔Java原始类型
  • kotlin.Boolean? is java.lang.Boolean Java type
  • kotlin.Boolean吗?. lang。布尔Java类型

In your test you created an adapter for a kotlin.Boolean (i.e., Java boolean type), while in your data model you have a kotlin.Boolean? (i.e., a java.lang.Boolean type). For this reason, when method create(...) of the Factory gets called, you are in a situation where type != requestedType, so your adapter is not created and the KotlinJsonAdapter is used instead. At this point, since the is_local field of the Json is not a boolean (but a string), Moshi should raise an Exception.

在您的测试中,您为一个kotlin创建了一个适配器。布尔(即。在您的数据模型中,您有一个kotlin.Boolean?(即。. lang。布尔类型)。由于这个原因,当调用工厂的method create(…)时,您处于type != requestedType的情况,因此没有创建适配器,而是使用KotlinJsonAdapter。此时,由于Json的is_local字段不是布尔(而是字符串),因此Moshi应该提出一个异常。

If you change your data model or your adapter to use the same type, you are in a situation where type == requestedType, so your adapter gets created, the exception is thrown as before, but you added a catch block that returns the default value.

如果您更改您的数据模型或适配器以使用相同的类型,那么您将处于type == requestedType的情况,因此您的适配器将被创建,异常将像以前一样抛出,但是您添加了一个catch块,该块返回默认值。