fastjson Fastjson反序列化kotlin data class对象,报default constructor not found. 错误

nue99wik  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(501)

Fastjson反序列化kotlin data class对象,参考Use Fastjson in Kotlin

运行demo:

data class DataClassSimple(val a : Int, val b : Int)

fun main(args: Array<String>) {

    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)
}

报错:

Connected to the target VM, address: '127.0.0.1:62501', transport: 'socket'
{"a":1,"b":2}
class com....test.DataClassSimple
Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class com.....test.DataClassSimple
Disconnected from the target VM, address: '127.0.0.1:62501', transport: 'socket'
	at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:467)
	at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:213)
	at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:660)
	at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:577)
	at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:388)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:658)
	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.....test.SerializableTestKt.main(SerializableTest.kt:20) //即:val dt2 = JSON.parseObject(jsons,clzs.javaObjectType)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题