Moshi 1.9.x无法序列化Kotlin类型

问题描述 投票:1回答:1

Moshi 1.9.1升级到1.8.0后,我遇到以下崩溃和堆栈跟踪:

java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.garpr.android.data.models.RankedPlayer. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.
for class com.garpr.android.data.models.RankedPlayer
for class com.garpr.android.data.models.AbsPlayer

    at com.squareup.moshi.Moshi$LookupChain.exceptionWithLookupStack(Moshi.java:349)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:150)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:98)
    at com.squareup.moshi.AdapterMethodsFactory$AdapterMethod.bind(AdapterMethodsFactory.java:313)
    at com.squareup.moshi.AdapterMethodsFactory.create(AdapterMethodsFactory.java:62)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:138)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:98)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:72)
    at com.garpr.android.data.converters.AbsPlayerConverterTest.setUp(AbsPlayerConverterTest.kt:76)
    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.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.garpr.android.data.models.RankedPlayer. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.
    at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:83)
    at com.squareup.moshi.Moshi.nextAdapter(Moshi.java:169)
    at com.squareup.moshi.AdapterMethodsFactory$AdapterMethod.bind(AdapterMethodsFactory.java:312)
    at com.squareup.moshi.AdapterMethodsFactory.create(AdapterMethodsFactory.java:62)
    at com.squareup.moshi.Moshi.adapter(Moshi.java:138)
    ... 23 more

我看到了this other Stack Overflow answer,但是它不适用于我,因为我已经在课程中添加了相关的@JsonClass(generateAdapter = X)行。

我为我的AbsPlayerConverter class类使用了自定义AbsPlayerConverter,因此可以使用它确定要解析到的子类。然后,如果解析为AbsPlayer,则为此使用另一个自定义转换器(AbsPlayer)。

这是我的Moshi-builder代码](RankedPlayer):

RankedPlayer

这里是RankedPlayerConverter中的莫西:

RankedPlayerConverter

所以最后,在所有这些文本和信息之后,我不知道如何解决此崩溃。我清楚地定义了如何对我的https://github.com/charlesmadere/smash-ranks-android/blob/a66d118f33c23b7a655d10da21630f909abd3639/smash-ranks-android/app/src/main/java/com/garpr/android/koin/MiscModule.kt#L26类进行序列化/反序列化。而且,如果我降级到Moshi 1.8.0,并完全保持我的代码库原样,此崩溃将消失并且一切都将正常进行。

有人有什么想法吗?

谢谢!

android json robolectric moshi
1个回答
0
投票

错误消息特别指出Moshi.Builder() .add(AbsPlayerConverter) .add(AbsRegionConverter) .add(AbsTournamentConverter) .add(MatchConverter) .add(RankedPlayerConverter) .add(SimpleDateConverter) .build()

根据my gradle file,如果不使用Moshi的代码源,则必须添加implementation "com.squareup.moshi:moshi:1.9.1" kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.1" 。根据RankedPlayer,这是Moshi 1.9中的特定行为更改。

Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact

并确保您使用的是Kotlin part of the readme

© www.soinside.com 2019 - 2024. All rights reserved.