6

假设有一个接口和 2 个(或更多)实现:

interface IRunnable {
  fun run()
}

class Horse : IRunnable {
  override fun run() {    println("horse running")  }
}

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

我想实现网络传输最简洁的JSON,即{"runnable":"H"}for Horse和{"runnable":"D"}for Dog

如果我无法修改接口和实现,我想将接口/实现序列化为 JSON,根据 Kotlin 的文档,我必须编写一个 自定义序列化程序,并使用它SerializersModule来实现目标。

假设只有HorseDog实施IRunnable,这就是我所做的:

class RunnableSerializer : KSerializer<IRunnable> {
  override val descriptor: SerialDescriptor
    get() = StringDescriptor.withName("runnable")


  override fun serialize(encoder: Encoder, obj: IRunnable) {
    val stringValue = when (obj) {
      is Horse -> { "H" }
      is Dog -> { "D" }
      else -> { null }
    }
    stringValue?.also {
      encoder.encodeString(it)
    }
  }

  override fun deserialize(decoder: Decoder): IRunnable {
    return decoder.decodeString().let { value ->
      when(value) {
        "H" -> Horse()
        "D" -> Dog()
        else -> throw RuntimeException("invalid $value")
      }
    }
  }
}

但是当我尝试将 a 转换Horse为 JSON ...

class RunnableTest {

  private val logger = KotlinLogging.logger { }

  @ImplicitReflectionSerializer
  @Test
  fun testJson() {
    val module1 = serializersModuleOf(IRunnable::class, RunnableSerializer())

    Json(context = module1).stringify(IRunnable::class.serializer(), Horse()).also {
      logger.info("json = {}", it)
    }
  }
}

它输出错误:

kotlinx.serialization.SerializationException: 
Can't locate argument-less serializer for class IRunnable. For generic classes, such as lists, please provide serializer explicitly.

在这种情况下如何实现类似 {"runnable":"H"}{"runnable":"D"}

谢谢。

环境:

<kotlin.version>1.3.60</kotlin.version>
<serialization.version>0.14.0</serialization.version>

更新,完整的错误信息:

kotlinx.serialization.SerializationException: Can't locate argument-less serializer for class IRunnable. For generic classes, such as lists, please provide serializer explicitly.

    at kotlinx.serialization.PlatformUtilsKt.serializer(PlatformUtils.kt:12)
    at RunnableTest.testJson(RunnableTest.kt:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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)
4

1 回答 1

6

您要实现的目标称为多态序列化,它支持 kotlinx.serialization。简而言之:它要求您IRunnableSerialModule. 对于您的代码,它应该如下所示:

val serialModule = SerializersModule {
    polymorphic(IRunnable::class) {
        Horse::class with HorseSerializer
        Dog::class with DogSerializer
    }
}

目前,您只 register IRunnable,正如异常正确表明的那样,无法初始化。你将如何初始化一个接口?你不能。相反,您想知道要初始化哪个特定的派生类型。这需要在 JSON 数据中嵌入类型信息,这就是多态序列化所做的。

此外,您不必实现自定义序列化程序。根据您链接到的文档,您可以为库类创建外部序列化程序。以下内容可能就足够了,因为 kotlinx.serialization 编译器插件会尝试为您生成合适的序列化程序,类似于@Serializable直接添加类:

@Serializer(forClass = Horse::class)
object HorseSerializer {}

@Serializer(forClass = Dog::class)
object DogSerializer {}
于 2019-12-23T14:09:35.667 回答