Moshi: Error when trying to deserialize data class with more than 32 constructor params/properties

Created on 11 Jun 2017  路  4Comments  路  Source: square/moshi

Okay, this is a weird one.
I have a rather large data class which has more than 32 constructor parameters/properties. When trying to deserialize a json for this class I get the following error:

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: This callable does not support a default call: public constructor DataClass(@com.squareup.moshi.Json par1: kotlin.String?, @com.squareup.moshi.Json par2: kotlin.String?, @com.squareup.moshi.Json par3: kotlin.String?, @com.squareup.moshi.Json par4: kotlin.String?, @com.squareup.moshi.Json par5: kotlin.String?, @com.squareup.moshi.Json par6: kotlin.String?, @com.squareup.moshi.Json par7: kotlin.String?, @com.squareup.moshi.Json par8: kotlin.String?, @com.squareup.moshi.Json par9: kotlin.String?, @com.squareup.moshi.Json par10: kotlin.String?, @com.squareup.moshi.Json par11: kotlin.String?, @com.squareup.moshi.Json par12: kotlin.String?, @com.squareup.moshi.Json par13: kotlin.String?, @com.squareup.moshi.Json par14: kotlin.String?, @com.squareup.moshi.Json par15: kotlin.String?, @com.squareup.moshi.Json par16: kotlin.String?, @com.squareup.moshi.Json par17: kotlin.String?, @com.squareup.moshi.Json par18: kotlin.String?, @com.squareup.moshi.Json par19: kotlin.String?, @com.squareup.moshi.Json par20: kotlin.String?, @com.squareup.moshi.Json par21: kotlin.String?, @com.squareup.moshi.Json par22: kotlin.String?, @com.squareup.moshi.Json par23: kotlin.String?, @com.squareup.moshi.Json par24: kotlin.String?, @com.squareup.moshi.Json par25: kotlin.String?, @com.squareup.moshi.Json par26: kotlin.String?, @com.squareup.moshi.Json par27: kotlin.String?, @com.squareup.moshi.Json par28: kotlin.String?, @com.squareup.moshi.Json par29: kotlin.String?, @com.squareup.moshi.Json par30: kotlin.String?, @com.squareup.moshi.Json par31: kotlin.String?, @com.squareup.moshi.Json par32: kotlin.String?, @com.squareup.moshi.Json par33: kotlin.String?) defined in DataClass[DeserializedClassConstructorDescriptor@1f97cf0d]

    at kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod(KCallableImpl.kt:152)
    at kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:111)
    at com.squareup.moshi.KotlinJsonAdapter.fromJson(KotlinJsonAdapter.kt:88)
    at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:128)
    at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:35)
    at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:39)
    at Tests.bug(Bug.kt:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

If I reduce the numer of parameters to 32 or below it all works and I can deserialize the json.

Gist with reproducable code: https://gist.github.com/hameno/b5f867bbe3a3484deffd1bac3ec515c9

I'm using moshi 1.5.0 and kotlin 1.1.2-3.

Maybe this is similar to https://github.com/FasterXML/jackson-module-kotlin/issues/46? Not sure if this is a bug in moshi or in Kotlin.

bug

Most helpful comment

dependencies {
    implementation("com.squareup.moshi:moshi-kotlin:1.5.0") {
        exclude group: "org.jetbrains.kotlin", module: "kotlin-reflect"
    }
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.20"
}

It can be fixed by this code, before moshi-1.6.0 release. Commit

All 4 comments

Looks like a bug in kotlin-reflect.
https://github.com/square/moshi/pull/324

This should be fixed now in 1.1.4

dependencies {
    implementation("com.squareup.moshi:moshi-kotlin:1.5.0") {
        exclude group: "org.jetbrains.kotlin", module: "kotlin-reflect"
    }
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.20"
}

It can be fixed by this code, before moshi-1.6.0 release. Commit

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ayedo picture ayedo  路  4Comments

VictorAlbertos picture VictorAlbertos  路  5Comments

vganin picture vganin  路  3Comments

Speedrockracer picture Speedrockracer  路  4Comments

tevjef picture tevjef  路  3Comments