Jetpack组合中断GSON

wz1wpwve  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(166)

每当我添加Jetpack Compose Dependencies我的GSON时,我的Android项目都无法正常工作。奇怪的是,如果我删除excludeFieldsWithModifiers,或者如果我没有为使用GSON解析的类使用任何继承,那么它就可以正常工作。
重现步骤:
1.添加Jetpack编写运行时Jetpack编写依赖项
1.添加GSON依赖项
1.使用GsonBuilder创建GSON对象并添加带有修饰符的排除字段选项
1.创建简单的类层次结构
1.尝试使用子类解析任何json,您在上面使用相同的GSON对象(在步骤3中创建)创建了该类
为了便于说明,我添加了一个样本

class Response(@SerializedName("id") var id : Int = 0) : BaseResponse()
open class BaseResponse

val gsonMapper: Gson by lazy {
GsonBuilder()
    .excludeFieldsWithModifiers(
        java.lang.reflect.Modifier.TRANSIENT. // Can you any other modifier
    )
    .create()
}

fun responseProcess() {
  try {
      val response = """{"id": 3}"""
      val data = gsonMapper.fromJson(response, Response::class.java)
      Log.d(TAG, "responseProcess: $data")
  } catch (e: Exception) {
      e.printStackTrace()
  }
}

上述代码的StackTrace

2022-11-30 18:14:12.162 14856-14856/com.bhaskar.myapplication W/System.err: java.lang.IllegalArgumentException: class com.bhaskar.myapplication.Response declares multiple JSON fields named $stable
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.google.gson.Gson.getAdapter(Gson.java:458)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:926)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:892)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:841)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:813)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.bhaskar.myapplication.MainActivityKt.responseProcess(MainActivity.kt:74)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.bhaskar.myapplication.MainActivityKt$Greeting$1$1.invoke(MainActivity.kt:54)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at com.bhaskar.myapplication.MainActivityKt$Greeting$1$1.invoke(MainActivity.kt:53)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke-k-4lQ0M(Clickable.kt:153)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke(Clickable.kt:142)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$1.invokeSuspend(TapGestureDetector.kt:223)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:328)
    2022-11-30 18:14:12.163 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:511)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.dispatchPointerEvent(SuspendingPointerInputFilter.kt:406)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:419)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:310)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:297)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:179)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:98)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:80)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1159)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1109)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1059)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3920)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3594)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:915)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1957)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at android.app.Activity.dispatchTouchEvent(Activity.java:4182)
    2022-11-30 18:14:12.164 14856-14856/com.bhaskar.myapplication W/System.err:     at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:873)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.View.dispatchPointerEvent(View.java:15458)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:7457)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:7233)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6595)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6786)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6843)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6652)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6618)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6626)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6599)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9880)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9718)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9671)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:10014)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:220)
    2022-11-30 18:14:12.165 14856-14856/com.bhaskar.myapplication W/System.err:     at android.os.MessageQueue.nativePollOnce(Native Method)
    2022-11-30 18:14:12.166 14856-14856/com.bhaskar.myapplication W/System.err:     at android.os.MessageQueue.next(MessageQueue.java:335)
    2022-11-30 18:14:12.166 14856-14856/com.bhaskar.myapplication W/System.err:     at android.os.Looper.loop(Looper.java:206)
    2022-11-30 18:14:12.166 14856-14856/com.bhaskar.myapplication W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:8633)
    2022-11-30 18:14:12.166 14856-14856/com.bhaskar.myapplication W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    2022-11-30 18:14:12.166 14856-14856/com.bhaskar.myapplication W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    2022-11-30 18:14:12.166 14856-14856/com.bhaskar.myapplication W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

谢谢您

hwazgwia

hwazgwia1#

通过调用excludeFieldsWithModifiers,你覆盖了默认的修饰符排除。因此,现在你(意外地?)包括了static字段,并且$stable似乎是一个合成字段added by the compiler(不确定为什么Gson没有检测到它是合成的)。
也可以通过将Modifier.STATIC添加到修改器来解决此问题:

excludeFieldsWithModifiers(Modifier.TRANSIENT or Modifier.STATIC)

(但请注意,这实际上与默认的Gson字段排除相同)
如果出于某种原因需要序列化static字段,则可能需要编写一个自定义ExclusionStrategy来忽略不需要的$stable字段。

相关问题