Skip to content
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

toJson exception in runtime #1736

Open
SwimLi opened this issue Sep 7, 2023 · 1 comment · May be fixed by #1840
Open

toJson exception in runtime #1736

SwimLi opened this issue Sep 7, 2023 · 1 comment · May be fixed by #1840
Labels

Comments

@SwimLi
Copy link

SwimLi commented Sep 7, 2023

@Keep
open class QualitieItemJsonData(

    @Json(name = "quality")
    val quality: String = "SUPER",

    @Json(name = "fps")
    val fps: Int = 24,

    @Json(name = "maxVideoBitrate")
    val maxVideoBitrate: Int? = null,

    @Json(name = "minVideoBitrate")
    val minVideoBitrate: Int? = null,

    @Json(name = "gop")
    val gop: Int? = null,
)

@Keep
class StatisConfig : QualitieItemJsonData {

    constructor():super()

    @Json(name = "h265Encode")
    var h265Encode: Boolean = false

    @Json(name = "h265Decode")
    var h265Decode: Boolean = false
}

private val moshi: Moshi = Moshi.Builder()
        .add(KotlinJsonAdapterFactory())
        .build()
val sta = StatisConfig()
moshi.adapter(StatisConfig::class.java).toJson(sta)

i get error: java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.learn.testkt.moshi.StatisConfig. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapterFactory from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.

but i modify like this,everything is well!??why?

@Keep
class StatisConfig : QualitieItemJsonData() {

    @Json(name = "h265Encode")
    var h265Encode: Boolean = false

    @Json(name = "h265Decode")
    var h265Decode: Boolean = false
}

kotlin version 1.7.10
implementation "com.squareup.moshi:moshi-kotlin:1.14.0"

@SwimLi SwimLi added the bug label Sep 7, 2023
@NightlyNexus
Copy link
Contributor

The KotlinJsonAdapterFactory requires the primary constructor, so the constructor():super() is breaking it. The error message should be better, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants