-
Notifications
You must be signed in to change notification settings - Fork 511
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
[Android环境下,JSONB解析报错,com.alibaba.fastjson2.JSONException: set address error, offset 17] #715
Comments
是因为Student类没有缺省构造函数,你可以用FieldBased模式试试看,比如: Student student = new Student("bin",12,"湖北");
byte[] bytes = JSONB.toBytes(student, JSONWriter.Feature.FieldBased);
Student student2 = JSONB.parseObject(bytes, Student.class, JSONReader.Feature.FieldBased); Android无法像服务器端那样,是因为在服务器端,是会把Class文件读取出来做分析,识别构造函数中的参数名,调用了一次非缺省的构造函数。Android上做不到,所以就功能对不齐了。 |
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.13.android-SNAPSHOT/ 需要在gradle中配置依赖:
|
引入implementation 'org.jetbrains.kotlin:kotlin-reflect:1.4.32'试试看 |
类似的还有@JSONField(serializeFeatures = [JSONWriter.Feature.NotWriteDefaultValue]) |
@Fish-Bin 发文本,按照图再输入一遍很烦人 |
data class Note(
) |
|
主要是data class不允许为空,使用Feature.FieldBased就好了 |
好的,我验证下 |
在对象头上也要加上@jsontype(serializeFeatures = [JSONWriter.Feature.FieldBased])这个吧,不然也还是报错 |
加了Feature.FieldBased 后 @jsontype的大部分注解都生效了,除了JSONWriter.Feature.NotWriteDefaultValue不生效,但是会导致@JSONField的注解都不生效了 |
https://github.com/alibaba/fastjson2/releases/tag/2.0.13 |
问题描述
JSONB.parseObject(bytes, Student.class)报错
环境信息
请填写以下信息:
重现步骤
如何操作可以重现该问题:
JSONB.parseObject(bytes, Student.class)
方法Student student = new Student("bin",12,"湖北"); byte[] bytes = JSONB.toBytes(student); Student student2 = JSONB.parseObject(bytes, Student.class); L.INSTANCE.i("Bin", student2.toString());
数据com.alibaba.fastjson2.JSONException: set address error, offset 17
错误期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
相关日志输出
com.alibaba.fastjson2.JSONException: set address error, offset 17
at com.alibaba.fastjson2.reader.FieldReaderStringMethod.readFieldValue(FieldReaderStringMethod.java:34)
at com.alibaba.fastjson2.reader.ObjectReader3.readJSONBObject(ObjectReader3.java:176)
at com.alibaba.fastjson2.JSONB.parseObject(JSONB.java:328)
at com.bin.jsonb.TestActivity.test1(TestActivity.java:45)
at com.bin.jsonb.TestActivity.access$000(TestActivity.java:16)
at com.bin.jsonb.TestActivity$1.onClick(TestActivity.java:25)
at android.view.View.performClick(View.java:7317)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
at android.view.View.performClickInternal(View.java:7291)
at android.view.View.access$3600(View.java:838)
at android.view.View$PerformClick.run(View.java:28247)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
附加信息
很好的一个库,最近想用它来优化项目存储,但是感觉对安卓支持力度很小,JSONB的一些基本功能都不能正常跑通
The text was updated successfully, but these errors were encountered: