Moshi 1.9.x 无法序列化 Kotlin 类型
Moshi 1.9.x Cannot serialize Kotlin type
我 运行 升级到 Moshi 1.9.1 (from 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.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.evaluate(RobolectricTestRunner.java:546)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=11=](SandboxTestRunner.java:252)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=11=](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.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
我看到了 ,但它不适用于我,因为我已经将相关的 @JsonClass(generateAdapter = X)
行添加到我的 classes.
我使用自定义 AbsPlayerConverter
class for my AbsPlayer
class, this is used so that I can determine which subclass to resolve to. And then, if it resolves to RankedPlayer
, I use another custom converter for that (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"
所以最后,在所有这些文本和信息之后,我不知道我怎么会 运行 陷入这场崩溃。我清楚地定义了如何 serialize/deserialize 我的 RankedPlayer
class。如果我降级到 Moshi 1.8.0,并让我的代码库完全保持原样,那么这次崩溃就会消失,一切都会完美无缺。
有人有什么想法吗?
谢谢!!
报错信息具体说Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact
根据 Kotlin part of the readme, you must add the KotlinJsonAdapterFactory
if you're not using Moshi's codegen. This was a specific behavior change in Moshi 1.9 as per the blog post about 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")
我正在使用改造,我必须执行以下操作:
在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()
当我忘记将 @JsonClass(generateAdapter = true)
添加到我的 类 之一时,我得到了这个。
我 运行 升级到 Moshi 1.9.1 (from 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.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.evaluate(RobolectricTestRunner.java:546)
at org.robolectric.internal.SandboxTestRunner.lambda$evaluate[=11=](SandboxTestRunner.java:252)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread[=11=](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.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
我看到了 @JsonClass(generateAdapter = X)
行添加到我的 classes.
我使用自定义 AbsPlayerConverter
class for my AbsPlayer
class, this is used so that I can determine which subclass to resolve to. And then, if it resolves to RankedPlayer
, I use another custom converter for that (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"
所以最后,在所有这些文本和信息之后,我不知道我怎么会 运行 陷入这场崩溃。我清楚地定义了如何 serialize/deserialize 我的 RankedPlayer
class。如果我降级到 Moshi 1.8.0,并让我的代码库完全保持原样,那么这次崩溃就会消失,一切都会完美无缺。
有人有什么想法吗?
谢谢!!
报错信息具体说Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact
根据 Kotlin part of the readme, you must add the KotlinJsonAdapterFactory
if you're not using Moshi's codegen. This was a specific behavior change in Moshi 1.9 as per the blog post about 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")
我正在使用改造,我必须执行以下操作:
在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()
当我忘记将 @JsonClass(generateAdapter = true)
添加到我的 类 之一时,我得到了这个。