The following works:
package com.squareup.moshi.problem
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
@JsonClass(generateAdapter = true)
data class Foo(
val bar: String?
)
fun main() {
val adapter=Moshi.Builder().build().adapter<Foo>(Foo::class.java)
adapter.fromJson("{}")
}
but when using a list it fails:
package com.squareup.moshi.problem
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
@JsonClass(generateAdapter = true)
data class Foo(
val bar: List<String>?
)
fun main() {
val adapter=Moshi.Builder().build().adapter<Foo>(Foo::class.java)
adapter.fromJson("{}")
}
with:
Exception in thread "main" com.squareup.moshi.JsonDataException: Required value 'bar' missing at $
at com.squareup.moshi.internal.Util.missingProperty(Util.java:605)
at com.squareup.moshi.problem.FooJsonAdapter.fromJson(FooJsonAdapter.kt:44)
at com.squareup.moshi.problem.FooJsonAdapter.fromJson(FooJsonAdapter.kt:16)
at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:40)
at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:43)
at com.squareup.moshi.problem.MoshiProblemKt.main(MoshiProblem.kt:13)
at com.squareup.moshi.problem.MoshiProblemKt.main(MoshiProblem.kt)
How can I make it work for a list? Here is a repo showing the problem: https://github.com/ligi/moshi_problem
The problem was actually this bug in moshi:
https://github.com/square/moshi/issues/990
hat tip @cketti for pointing me to the issue.