1

我试图让自定义序列化与 kotlinx.serialization 一起工作,就像这个简化的例子:

@Serializable
open class KdfParams {
    var dklen: Int = 0
    var salt: String? = null

    @Serializer(forClass = KdfParams::class)
    companion object {
        override fun save(output: KOutput, obj: KdfParams) = TODO()

        override fun load(input: KInput): KdfParams {
            return KdfParams()
        }
    }

}

但我得到:

java.lang.IllegalArgumentException: JSON at 369: Expected '[, kind: POLYMORPHIC'

    at kotlinx.serialization.json.JSONKt.fail(JSON.kt:525)
    at kotlinx.serialization.json.JSONKt.require(JSON.kt:521)
    at kotlinx.serialization.json.JSONKt.access$require(JSON.kt:1)
    at kotlinx.serialization.json.JSON$JsonInput.readBegin(JSON.kt:232)
    at kotlinx.serialization.PolymorphicSerializer.load(PolymorphicSerializer.kt:37)
    at kotlinx.serialization.KInput.readSerializableValue(Serialization.kt:243)
    at kotlinx.serialization.KInput.readNullableSerializableValue(Serialization.kt:246)
    at kotlinx.serialization.ElementValueInput.readNullableSerializableElementValue(Serialization.kt:441)
    at org.kethereum.wallet.WalletFileCrypto$$serializer.load(WalletFile.kt)
    at org.kethereum.wallet.WalletFileCrypto$$serializer.load(WalletFile.kt:13)
    at kotlinx.serialization.KInput.readSerializableValue(Serialization.kt:243)
    at kotlinx.serialization.KInput.readNullableSerializableValue(Serialization.kt:246)
    at kotlinx.serialization.ElementValueInput.readNullableSerializableElementValue(Serialization.kt:441)
    at org.kethereum.wallet.WalletFile$$serializer.load(WalletFile.kt)
    at org.kethereum.wallet.WalletFile$$serializer.load(WalletFile.kt:29)
    at kotlinx.serialization.KInput.read(Serialization.kt:209)
    at kotlinx.serialization.json.JSON.parse(JSON.kt:49)
    at kotlinx.serialization.json.JSON$Companion.parse(JSON.kt:59)
    at org.kethereum.wallet.WalletTest.load(WalletTest.kt:53)
    at org.kethereum.wallet.WalletTest.testDecryptAes128Ctr(WalletTest.kt:33)
    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.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
4

1 回答 1

0

这很可能是由于您的类如何相互继承。如果您的可序列化类都扩展了另一个可序列化的类,则可能会发生此错误。发布您的代码以查看这是否是错误。

于 2018-06-22T11:38:40.617 回答