alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.75k stars 6.5k forks source link

使用kotlin的data class 时会报错:没有默认构造器 #1607

Open KingLanding94 opened 6 years ago

KingLanding94 commented 6 years ago

用的是kotlin使用范例上面的内容,但是程序依然会崩溃。期待解决。

data class DataClass(@JSONField(name="aa")val a : Int, @JSONField(name="bb")val b : Int)

        val dt = DataClass(1,2)
        val json = JSON.toJSONString(dt)
        logger.e(json)
        val clz = DataClass::class
        logger.e(clz.javaObjectType.toString())
        val dt1 = JSON.parseObject(json,clz.javaObjectType)
        logger.e(dt1.toString())

出错内容: Process: cn.ac.ia.iot.www.telemedicine, PID: 8718 com.alibaba.fastjson.JSONException: default constructor not found. class cn.ac.ia.iot.www.telemedicine.mvp.model.bean.DataClass at com.alibaba.fastjson.parser.JavaBeanInfo.build(JavaBeanInfo.java:496) at com.alibaba.fastjson.parser.JavaBeanDeserializer.(JavaBeanDeserializer.java:35) at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:229) at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:148) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:683) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:659) at com.alibaba.fastjson.JSON.parseObject(JSON.java:238) at com.alibaba.fastjson.JSON.parseObject(JSON.java:210) at com.alibaba.fastjson.JSON.parseObject(JSON.java:169) at com.alibaba.fastjson.JSON.parseObject(JSON.java:278) at cn.ac.ia.iot.www.telemedicine.mvp.presenter.ActivityLoginPresenter.attemptLogin(ActivityLoginPresenter.kt:44) at cn.ac.ia.iot.www.telemedicine.ui.activity.LoginActivity.attemptLogin(LoginActivity.kt:74) at cn.ac.ia.iot.www.telemedicine.ui.activity.LoginActivity.onClick(LoginActivity.kt:88) at android.view.View.performClick(View.java:4790) at android.view.View$PerformClick.run(View.java:19924) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5418) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1037) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

KingLanding94 commented 6 years ago

报错时使用的是 fastjson:1.1.67.android

改成 fastjson:1.2.41 程序就可以正常运行了。

StanNing commented 6 years ago

这个问题经过验证依然存在, @wenshao 问一下什么版本可以解决啊?data class 使用的场景实在太多了

wenshao commented 6 years ago

@StanNing 能提供重现问题的testcase么?

StanNing commented 6 years ago

@wenshao data class JsonDemo1(var a: JsonDemo2 = JsonDemo2()) data class JsonDemo2(var b: String = "")

    @Test
    fun test2() {
        val s1 = "{\"a\":{\"b\":123}}"
        val demo = s1.toJsonObject(JsonDemo1::class.java)
        logd(demo?.toJson())
    }
fun <T> String.toJsonObject(parseClass: Class<T>): T? {
    var t: T? = null
    try {
        t = JSON.parseObject(this, parseClass)
    } catch (e: Exception) {
        loge(e)
    }
    return t
}

    fun Any.toJson(): String {
        var jsonStr = ""
        try {
            jsonStr = JSON.toJSONString(this) //现在替换为FastJson
        } catch (e: Exception) {
            loge(e)
        }
        return jsonStr
    }

在Android项目中
implementation "com.alibaba:fastjson:1.2.41" 上面报错 default constructor not found.
换回1.2.31就正常了。

compile 'com.alibaba:fastjson:1.1.67.android' 使用Android平台的同样会出现上面这个错误。
Lua12138 commented 6 years ago

@wenshao the same issue

Moyuchen commented 5 years ago

解决了,加一行compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version",使用的时1.2.54

GuitarDian commented 5 years ago

楼上兄弟厉害。我试过noarg,allopen,都配置不了。 implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 完美解决,版本1.3.40

ifynn commented 4 years ago

@wenshao @GuitarDian 为什么我加了implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" ,还是会报 default constructor not found

REBOOTERS commented 1 year ago

依赖添加 implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 之后,依然报 default constructor not found 错误的,可以在混淆配置添加

-keepattributes *Annotation*
-keep class kotlin.** { *; }
-keep class org.jetbrains.** { *; }

亲测有效