Moshi: Generic Kotlin data classes cannot be (de)serialized

Created on 18 May 2017  Â·  19Comments  Â·  Source: square/moshi

It seems that Moshi 1.5.0 has a problem with (de)serialization of generic data classes.

data class Test<out T>(val item: T)

data class Test2(val test: Test<String>)

The following code

val json = """{test:{item:"hello"}}"""
val moshi = Moshi.Builder()
        .add(KotlinJsonAdapterFactory())
        .build()
val data = moshi.adapter(Test2::class.java).fromJson(json)

produces the exception

java.lang.IllegalArgumentException: Expected a Class, ParameterizedType, or GenericArrayType, but <null> is of type null
  at com.squareup.moshi.Types.getRawType(Types.java:167)
  at com.squareup.moshi.ClassJsonAdapter$1.createFieldBindings(ClassJsonAdapter.java:83)
  at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:75)
  at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
  at com.squareup.moshi.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:184)
  at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
  at com.squareup.moshi.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:184)
  at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
  at com.squareup.moshi.Moshi.adapter(Moshi.java:62)
  at com.svenjacobs.test.components.main.view.MainActivity.onCreate(MainActivity.kt:24)

This was working in Moshi 1.4.0 and before.

bug

Most helpful comment

Can fix.

All 19 comments

Can fix.

Not sure if related but I'm also getting the same issue using Lists. As an example:

data class PaginatedResponse<out T>(val page: Int, val results: List<T>)

Is there a suggested workaround until this issue gets fixed?

@thatJavaNerd I reverted to version 1.4.0

Anyone knows when the PR above fixing this issue will be merged?

Thanks.

I think it’s too simple. Will try to find time for it.

@swankjesse Is this fix being worked on still?

Yep.

@swankjesse Any news regarding this? Just wondering if to use Moshi 1.4.0 while this is fixed.

Ping, this issue is quite critical.

Please fix this

Is this bug maintained by now?

I guess the issue is not present in kotshi as this fixed issue shows: https://github.com/ansman/kotshi/issues/41

For that kind of data class, use java to avoid this bug temporarily

What I ended up doing was using AutoValue + auto-value-moshi. You don't need reflection so it's gonna be a lot faster if you need to deserialize a ton of models very quickly like JRAW

I can confirm that the Kotshi library solves this problem and, moreover, saves us from having to use moshi-kotlin and, therefore, saves us from kotlin-reflect, which contains >10k methods. Which is an important consideration for an Android project.

fixed by #475 for users of the KotlinJsonAdapterFactory.

When to release the latest fix?

I can verify this has been fixed for KotlinJsonAdapter in 1.6.0-RC1 for the following sample.

data class Response<Output : Any>(
    val id: String?,
    val result: Output
)

val adapter: JsonAdapter<Response<List<String>>> = moshi.adapter(
    Types.newParameterizedType(
        Response::class.java,  
        Types.newParameterizedType(
            List::class.java, 
            String::class.java
        )
    )
)

adapter.toJson(Response("foo", listOf("bar", "baz"))) // yields: {"id":"foo","result":["bar","baz"]}
Was this page helpful?
0 / 5 - 0 ratings