Moshi适配器:跳过List<T>中的坏对象

6

我使用Moshi,并需要解决后端错误的问题。有时,当我请求对象列表时,其中一些对象不包含必填字段。当然,我可以捕获和处理JsonDataException,但我希望跳过这些对象。如何使用Moshi实现?

更新

我有几个模型用于我的任务。

@JsonClass(generateAdapter = true)
data class User(
        val name: String,
        val age: Int?
)

@JsonClass(generateAdapter = true)
data class UserList(val list: List<User>)

以及有漏洞的JSON

{
  "list": [
    {
      "name": "John",
      "age": 20
    },
    {
      "age": 18
    },
    {
      "name": "Jane",
      "age": 21
    }
  ]
}

正如您所看到的,第二个对象没有强制要求的 name 字段,我希望通过 Moshi 适配器跳过它。


请发布您设置Moshi适配器的代码。 - SharpMobileCode
@SharpMobileCode 我没有适配器的代码。我只是想写它 =) 但我可以展示带有模型的代码。 - 0wl
3个回答

5

在这个解决方案中有一个陷阱,它只捕获并忽略失败后的内容。如果您的元素适配器在错误后停止读取,那么读者可能正在读取一个嵌套对象的中间部分,例如,下一个hasNext调用将在错误的位置调用。

就像Jesse提到的那样,您可以查看并跳过整个值。

class SkipBadElementsListAdapter(private val elementAdapter: JsonAdapter<Any?>) :
    JsonAdapter<List<Any?>>() {
  object Factory : JsonAdapter.Factory {
    override fun create(type: Type, annotations: Set<Annotation>, moshi: Moshi): JsonAdapter<*>? {
      if (annotations.isNotEmpty() || Types.getRawType(type) != List::class.java) {
        return null
      }
      val elementType = Types.collectionElementType(type, List::class.java)
      val elementAdapter = moshi.adapter<Any?>(elementType)
      return SkipBadElementsListAdapter(elementAdapter)
    }
  }

  override fun fromJson(reader: JsonReader): List<Any?>? {
    val result = mutableListOf<Any?>()
    reader.beginArray()
    while (reader.hasNext()) {
      try {
        val peeked = reader.peekJson()
        result += elementAdapter.fromJson(peeked)
      } catch (ignored: JsonDataException) {
      }
      reader.skipValue()
    }
    reader.endArray()
    return result

  }

  override fun toJson(writer: JsonWriter, value: List<Any?>?) {
    if (value == null) {
      throw NullPointerException("value was null! Wrap in .nullSafe() to write nullable values.")
    }
    writer.beginArray()
    for (i in value.indices) {
      elementAdapter.toJson(writer, value[i])
    }
    writer.endArray()
  }
}

当调用create时,使用PolymorphicJsonAdapterFactory实现会崩溃。 com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory.create(PolymorphicJsonAdapterFactory.java:216) - user3354265

3

看起来我找到了答案

class SkipBadListObjectsAdapterFactory : JsonAdapter.Factory {
    override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
        return if (annotations.isEmpty() && Types.getRawType(type) == List::class.java) {
            val elementType = Types.collectionElementType(type, List::class.java)
            val elementAdapter = moshi.adapter<Any>(elementType)

            SkipBadListObjectsAdapter(elementAdapter)
        } else {
            null
        }
    }

    private class SkipBadListObjectsAdapter<T : Any>(private val elementAdapter: JsonAdapter<T>) :
        JsonAdapter<List<T>>() {
        override fun fromJson(reader: JsonReader): List<T>? {
            val goodObjectsList = mutableListOf<T>()

            reader.beginArray()

            while (reader.hasNext()) {
                try {
                    elementAdapter.fromJson(reader)?.let(goodObjectsList::add)
                } catch (e: JsonDataException) {
                    // Skip bad element ;)
                }
            }

            reader.endArray()

            return goodObjectsList

        }

        override fun toJson(writer: JsonWriter, value: List<T>?) {
            throw UnsupportedOperationException("SkipBadListObjectsAdapter is only used to deserialize objects")
        }
    }
}

感谢“其他主题的人们”=),他们对IT技术相关内容做出了贡献。

考虑使用JsonReader.peekJson()获取当前不消耗的JsonReader的副本。即使嵌套对象格式不正确,您也可以使用skip()跳过整个值。 - Jesse Wilson
我在使用 Kotlin 泛型时遇到了问题。我一直收到的异常是:java.lang.IllegalArgumentException: No JsonAdapter for E (with no annotations)。如果有人知道解决方法,那将不胜感激!此外,我认为 Moshi 应该提供这种功能作为一个注释或其他形式的开箱即用功能。因为在许多其他解析器中,这是非常常见的。 - Bueno

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接