fastjson Can't find default constructor when deserializing Kotlin data classes

67up9zun  于 2021-11-27  发布在  Java
关注(0)|答案(4)|浏览(282)

I meet exceptions when I deserialize Kotlin data classes with following code:

data class DataClassSimple(val a : Int, val b : Int)
    @Test
    fun test2() {
        val dts = DataClassSimple(1,2)
        val jsons = JSON.toJSONString(dts)
        println(jsons)
        val clzs = DataClassSimple::class
        println(clzs.javaObjectType)
        val dt2 = JSON.parseObject(jsons,clzs.javaObjectType)
        println(dt2)
    }

Exception is listed below:

com.alibaba.fastjson.JSONException: default constructor not found. class com.example.HelloTest$DataClassSimple

	at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:465)
	at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:211)
	at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:643)
	at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:560)
	at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:373)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:651)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:488)
	at com.example.HelloTest.test2(HelloTest.kt:29)

Environment:

  1. Kotlin 1.2.10
  2. JDK 1.8
  3. fastjson 1.2.44

Thank you!

flvlnr44

flvlnr441#

@wenshao Can you have a look on this ?

jbose2ul

jbose2ul2#

I have the same problem too

aor9mmx1

aor9mmx13#

same problem here too. Creating empty constructors doesn't help

z9smfwbn

z9smfwbn4#

so, how to fix this bug? use gson ?

相关问题