kotlin使用com.alibaba.fastjson.JSONException: default constructor not found
来源:互联网 发布:数据科学网 编辑:程序博客网 时间:2024/05/22 12:58
使用kotlin,在用fastjson来解析json数据时,出现这个错误
目前fastjson最新版本是1.1.62,不过还不稳定,出现这个问题建议使用1.1.61
github地址:https://github.com/ttarfall/fastjson
目前这个错误在使用kotlin的data数据类和普通数据类都会有,解决方法:
- 使用1.1.61稳定版
- 无使用 普通数据类 并且像这样多增加一个空构造器
class UserInfo{ constructor() constructor(email: String, id: String, name: String)}
期待官网能尽快更新这个问题
那下边我们来源码分析一下出现这个错误的原因:(这里主要比较的1.1.61和1.1.62)
由于官网是从1.1.61开始兼容kotlin解析,之前版本如果解析出错正常,因为没兼容,可能会有莫名其妙的错误,哈哈~~~
我们的异常是从这个方法抛出的:
public static JavaBeanInfo build(Class<?> clazz, // int classModifiers, // Type type, // boolean fieldOnly, // boolean jsonTypeSupport, // boolean jsonFieldSupport, // boolean fieldGenericSupport, // PropertyNamingStrategy propertyNamingStrategy )
具体代码在1.1.62在这里判断
if ((classModifiers & Modifier.ABSTRACT) == 0 && !kotlin) {
如果看过1.1.61的源码,这里唯一的区别就是多了一个kotlin的判断
在1.1.61直接没有这个判断,执行该判断内容创建constructor,但是这个是java代码,kotlin的数据data类默认是没有空的构造器,Java会为我们默认生成一个constructor,所以kotlin这里使用1.1.61要默认添加一个构造器
那为什么1.1.62执行else方法有没有成功呢?还是上源代码
关键方法
public static String[] getKoltinConstructorParameters(Class clazz)
在判断kotlin_kclass_constructor为空的时候要去创建一个,关键代码:
try { Class class_kotlin_kclass = Class.forName("kotlin.reflect.jvm.internal.KClassImpl"); kotlin_kclass_constructor = class_kotlin_kclass.getConstructor(Class.class); kotlin_kclass_getConstructors = class_kotlin_kclass.getMethod("getConstructors"); Class class_kotlin_kfunction = Class.forName("kotlin.reflect.KFunction"); kotlin_kfunction_getParameters = class_kotlin_kfunction.getMethod("getParameters"); Class class_kotlinn_kparameter = Class.forName("kotlin.reflect.KParameter"); kotlin_kparameter_getName = class_kotlinn_kparameter.getMethod("getName"); } catch (Throwable e) { kotlin_class_klass_error = true; }
细心的朋友可以发现这里通过反射区创建,结构就抛出了异常,是因为找不到这个类
kotlin.reflect.jvm.internal.KClassImpl
我也是醉了,期待官网尽快更新了,我用的kotlin1.1.4确实找不到这个实现类
阅读全文
0 0
- kotlin使用com.alibaba.fastjson.JSONException: default constructor not found
- com.alibaba.fastjson.JSONException: default constructor not found
- com.alibaba.fastjson.JSONException: default constructor not found. class com.anerfa.axdblue_code.ho
- Android开发fastjson错误:JSONException: default constructor not found. class com.xx...
- fastjson混淆后default constructor not found.
- com.alibaba.fastjson.JSONException: not close json text, token : error
- com.alibaba.fastjson.JSONException: not close json text, token : error
- com.alibaba.fastjson.JSONException: not close json text, token : :
- 解决com.alibaba.fastjson.JSONException: autoType is not support
- com.alibaba.fastjson.JSONException: error parse e
- com.alibaba.fastjson.JSONException: unclosed string
- com.alibaba.fastjson.JSOnException:exepct '[' ,but {
- com.alibaba.fastjson.JSOnException:exepct '[' ,but {
- fastjson解析json时报错default constructor not found. class.............
- fastjson解析json时报错default constructor not found. class............
- com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0
- com.alibaba.fastjson.JSONException: syntax error, pos 1
- com.alibaba.fastjson.JSONException: syntax error, expect {, actual error
- opengl 写入数据
- Transimission 操作及设置
- 教程:基于访问控制的ABAP CDS视图权限
- Mybatis中的 ${ } 和 #{ }的区别
- HTML表格与框架
- kotlin使用com.alibaba.fastjson.JSONException: default constructor not found
- 转行学Java有可能成为高薪大牛吗?
- 《剑指Offer》做题总结(三)
- python opencv去图片水印
- WebService 四种发布方式总结
- Hadoop源码解读-Job初始化过程
- Bound Found POJ
- (八)监听耳机插入拔出状态
- 线性链表:单链