假设有一个接口和 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
来实现目标。
假设只有Horse
和Dog
实施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)