从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类使用了自定义AbsPlayer
,使用它是为了确定要解析的子类。然后,如果解析为RankedPlayer
,我将为此使用另一个自定义转换器(RankedPlayerConverter
)。
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.8.0,并完全保持我的代码库原样,此崩溃将消失,并且一切都将正常进行。
有人有什么想法吗?
谢谢!
答案 0 :(得分:5)
错误消息特别指出Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact
根据Kotlin part of the readme,如果不使用Moshi的代码源,则必须添加KotlinJsonAdapterFactory
。根据{{3}},这是Moshi 1.9中的特定行为更改。
Moshi.Builder()
.add(AbsPlayerConverter)
.add(AbsRegionConverter)
.add(AbsTournamentConverter)
.add(MatchConverter)
.add(RankedPlayerConverter)
.add(SimpleDateConverter)
.add(KotlinJsonAdapterFactory())
.build()
并确保您使用的是implementation("com.squareup.moshi:moshi-kotlin:1.9.1")
答案 1 :(得分:4)
我正在使用改造,我必须执行以下操作:
在 build.grade 中:
implementation "com.squareup.moshi:moshi-kotlin:$moshiVersion"
在我的存储库中:
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val retrofit = Retrofit.Builder()
.baseUrl(WEB_SERVICE_URL)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()