What does `moshi` in the adapter-constructor generated by codegen mean?

与世无争的帅哥 提交于 2019-12-20 05:15:43

问题


I'm a newbee in kotlin-android development.

I want to parse a JSON like

{
  "name": "This is my name",
}

to a Foo class defined as

Foo.kt

@JsonClass(generateAdapter = true)
data class Foo(val name: String)

This Foo class file generates a code by codegen as follows.

FooJsonAdapter.kt (generated by codegen)

class FooJsonAdapter(moshi: Moshi) : JsonAdapter<Foo>() {
    private val options: JsonReader.Options

    private val stringAdapter: JsonAdapter<String>

    override fun toString(): String

    override fun fromJson(reader: JsonReader): Foo 

    override fun toJson(writer: JsonWriter, value: Foo?) 
}

The FooJsonAdapter class's constructor specifies Moshi object, and here is my question. What kind of Moshi object should be passed to the constructor? I tried the following, but this does not work.

MainActivity.kt

Moshi.Builder().add(
    FooJsonAdapter(
        Moshi.Builder().build()
    )
).build()

Error message

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hoge, PID: 31816
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hoge/com.example.hoge.MainActivity}: java.lang.IllegalArgumentException: Expected at least one @ToJson or @FromJson method on com.example.hoge.FooJsonAdapter
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
     Caused by: java.lang.IllegalArgumentException: Expected at least one @ToJson or @FromJson method on com.example.hoge.FooJsonAdapter
        at com.squareup.moshi.AdapterMethodsFactory.get(AdapterMethodsFactory.java:137)
        at com.squareup.moshi.Moshi$Builder.add(Moshi.java:237)
        at com.example.hoge.MainActivity.onCreate(MainActivity.kt:14)
        at android.app.Activity.performCreate(Activity.java:7783)
        at android.app.Activity.performCreate(Activity.java:7772)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7319) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 

What should I do? Thx!


回答1:


You should be using Moshi.Builder.add(Type, JsonAdapter) instead of Moshi.Builder.add(JsonAdapter)



来源:https://stackoverflow.com/questions/56984743/what-does-moshi-in-the-adapter-constructor-generated-by-codegen-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!