generics - 具有 kotlin 泛型的 Moshi 为接口(interface)抛出 No JsonAdapter

标签 generics kotlin type-erasure moshi

假设我有一个接口(interface) IRunnable和两个实现 CatDog :

interface IRunnable {
  fun run()
}

class Cat : IRunnable {
  override fun run() { println("cat running") }
}

class Dog : IRunnable {
  override fun run() { println("dog running") }
}

并且有一个接口(interface)可以转换IRunnableMap<String,String>键= runnable

interface IMapConverter<T> {
  fun getMap(impl : T) : Map<String , String>
  fun getImpl(map : Map<String , String>) : T?
}
class RunnableConverter : IMapConverter<IRunnable> {
  private val key = "runnable"

  override fun getMap(impl: IRunnable): Map<String, String> {
    val value = when(impl) {
      is Cat -> "C"
      is Dog -> "D"
      else -> throw RuntimeException("error")
    }
    return mapOf(key to value)
  }

  override fun getImpl(map: Map<String, String>): IRunnable? {
    return map[key]?.let { value ->
      when (value) {
        "C" -> Cat()
        "D" -> Dog()
        else -> throw RuntimeException("error")
      }
    }
  }
}

然后使用 moshi ,我想要 IMapConverter成为JsonAdapter ,所以我添加了一个 getAdapter()内部功能RunnableConverter :

fun getAdapter() : JsonAdapter<IRunnable> {
    return object : JsonAdapter<IRunnable>() {

      @ToJson
      override fun toJson(writer: JsonWriter, runnable: IRunnable?) {
        runnable?.also { impl ->
          writer.beginObject()
          getMap(impl).forEach { (key , value) ->
            writer.name(key).value(value)
          }
          writer.endObject()
        }
      }

      @FromJson
      override fun fromJson(reader: JsonReader): IRunnable? {
        reader.beginObject()

        val map = mutableMapOf<String , String>().apply {
          while (reader.hasNext()) {
            put(reader.nextName() , reader.nextString())
          }
        }
        val result = getImpl(map)

        reader.endObject()
        return result
      }
    }
  }

效果不错:

  @Test
  fun testConverter1() {
    val converter = RunnableConverter()

    val moshi = Moshi.Builder()
      .add(converter.getAdapter())
      .build()

    val adapter = moshi.adapter<IRunnable>(IRunnable::class.java)
    adapter.toJson(Dog()).also { json ->
      assertEquals("""{"runnable":"D"}""" , json)
      adapter.fromJson(json).also { runnable ->
        assertTrue(runnable is Dog)
      }
    }
  }

但是,当我想将适配器外部化到扩展函数时:

fun <T> IMapConverter<T>.toJsonAdapter() : JsonAdapter<T> {
  return object : JsonAdapter<T>() {

    @ToJson
    override fun toJson(writer: JsonWriter, value: T?) {
      value?.also { impl ->
        writer.beginObject()
        getMap(impl).forEach { (key , value) ->
          writer.name(key).value(value)
        }
        writer.endObject()
      }
    }

    @FromJson
    override fun fromJson(reader: JsonReader): T? {
      reader.beginObject()

      val map = mutableMapOf<String , String>().apply {
        while (reader.hasNext()) {
          put(reader.nextName() , reader.nextString())
        }
      }

      val result = getImpl(map)
      reader.endObject()
      return result
    }
  }
}

当我想测试这个功能时:

  @Test
  fun testConverter2() {
    val converter = RunnableConverter()

    val moshi = Moshi.Builder()
      .add(converter.toJsonAdapter()) // this is extension function
      .build()

    val adapter = moshi.adapter<IRunnable>(IRunnable::class.java)
    adapter.toJson(Dog()).also { json ->
      logger.info("json of dog = {}", json)
      assertEquals("""{"runnable":"D"}""" , json)
      adapter.fromJson(json).also { runnable ->
        assertTrue(runnable is Dog)
      }
    }
  }

编译正常,但报错:

java.lang.IllegalArgumentException: No JsonAdapter for interface moshi.IRunnable (with no annotations)

    at com.squareup.moshi.Moshi.adapter(Moshi.java:148)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:98)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:72)
    at moshi.MoshiTest.testConverter2(MoshiTest.kt:39)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

我尝试在 Moshi.java 的第 137 行设置断点 https://github.com/square/moshi/blob/40a829ef181e7097087ec0e95cdcf3e3fbba3156/moshi/src/main/java/com/squareup/moshi/Moshi.java#L137

这是调试截图:

OK 一个(在 RunnableConverter 中定义): enter image description here

这是由扩展函数创建的通用版本: enter image description here

似乎因为类型删除,它不能工作...... 有什么办法可以解决吗?

对于任何对此感兴趣的人,完整代码在这里: https://gist.github.com/smallufo/985db4719c5434a5f57f06b14011de78

环境:

<dependency>
  <groupId>com.squareup.moshi</groupId>
  <artifactId>moshi-kotlin</artifactId>
  <version>1.9.2</version>
</dependency>

谢谢。

最佳答案

选项 1(我希望它能工作,但不确定):尝试更改 toJsonAdapterinline fun <reified T> IMapConverter<T>.toJsonAdapter() : JsonAdapter<T> { ... } .

选项 2:使用 Moshi.Builder.add 之一允许指定类型的重载( Class<T> extends Type ):

.add(IRunnable::class.java, converter.toJsonAdapter())

为避免给出错误的类型,您可以使用 reified再次:

inline fun <reified T> Moshi.Builder.add(converter: IMapConverter<T>) = add(T::class.java, converter.toJsonAdapter())

然后

Moshi.Builder().add(converter)

关于generics - 具有 kotlin 泛型的 Moshi 为接口(interface)抛出 No JsonAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59526697/

相关文章:

Java泛型可分配性

c# - 通用接口(interface),通用接口(interface)与泛型

java - 仪器测试将在 Android Studio 3(空测试套件)上作为本地单元测试运行

java - 冲突的 jetified-kotlin-reflect 依赖

scala - 在 Scala 中对 HashMap 进行子类化,解决类型删除问题

Java:有界类型的 getClass()

Java 泛型 : Infering the type from another method?

c# - 为什么 Generic Casting 不适用于这部分代码?

Android Kotlin 改造协程请求出现 moshi 错误

ios - 无法将类型 'AnyDataSource<NSManagedObjectSubclass>' 的值分配给类型 'AnyDataSource<NSManagedObject>' 的值