New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Kotlin data classes cannot be (de)serialized #309

Closed
svenjacobs opened this Issue May 18, 2017 · 19 comments

Comments

@svenjacobs

svenjacobs commented May 18, 2017

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.

@swankjesse

This comment has been minimized.

Member

swankjesse commented May 18, 2017

Can fix.

@fmrsabino

This comment has been minimized.

fmrsabino commented May 30, 2017

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>)
@mattbdean

This comment has been minimized.

mattbdean commented Jun 18, 2017

Is there a suggested workaround until this issue gets fixed?

@fmrsabino

This comment has been minimized.

fmrsabino commented Jun 18, 2017

@thatJavaNerd I reverted to version 1.4.0

@cre8ivejp

This comment has been minimized.

cre8ivejp commented Jul 26, 2017

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

Thanks.

@swankjesse

This comment has been minimized.

Member

swankjesse commented Aug 1, 2017

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

@rodly

This comment has been minimized.

rodly commented Aug 28, 2017

@swankjesse Is this fix being worked on still?

@swankjesse

This comment has been minimized.

Member

swankjesse commented Aug 28, 2017

Yep.

@mradzinski

This comment has been minimized.

mradzinski commented Oct 30, 2017

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

@ishitatsuyuki

This comment has been minimized.

ishitatsuyuki commented Nov 8, 2017

Ping, this issue is quite critical.

@mhf-air

This comment has been minimized.

mhf-air commented Dec 24, 2017

Please fix this

@blackberry2016

This comment has been minimized.

blackberry2016 commented Feb 1, 2018

Is this bug maintained by now?

@LouisCAD

This comment has been minimized.

Contributor

LouisCAD commented Feb 8, 2018

I guess the issue is not present in kotshi as this fixed issue shows: ansman/kotshi#41

@blackberry2016

This comment has been minimized.

blackberry2016 commented Feb 15, 2018

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

@mattbdean

This comment has been minimized.

mattbdean commented Feb 15, 2018

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

@autonomousapps

This comment has been minimized.

autonomousapps commented Mar 5, 2018

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.

@NightlyNexus

This comment has been minimized.

Collaborator

NightlyNexus commented Apr 4, 2018

fixed by #475 for users of the KotlinJsonAdapterFactory.

@AllenCoder

This comment has been minimized.

AllenCoder commented May 2, 2018

When to release the latest fix?

@bradynpoulsen

This comment has been minimized.

bradynpoulsen commented May 10, 2018

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"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment