Skip to content
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

[BUG]单元素的数组json序列化的时候,没有中括号;后面直接导致解析端用fastjon反序列化的时候,直接报格式错误 #426

Closed
freedomwolf008 opened this issue Jun 7, 2022 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@freedomwolf008
Copy link

freedomwolf008 commented Jun 7, 2022

问题描述

单元素的数组json序列化的时候,没有中括号;后面直接导致解析端用fastjon反序列化的时候,直接报格式错误

环境信息

请填写以下信息:

  • OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:HotSpot jdk 17.0.3]
  • 版本信息:[e.g.:Fastjson2 2.0.4]

重现步骤

如何操作可以重现该问题:
一些框架在序列化的时候,如果是单元素的集合,直接就是省去了中括号,后面直接导致解析端用fastjon2反序列化的时候,直接报格式错误

{"name": "zhangsan", "books" : "西游记"}

{"name": "zhangsan", "books" : ["西游记","红楼梦"]}

  1. 使用 xxx.xxx 方法
  2. 输入 ... 数据
  3. 出现 ... 错误
//可在此输入示例代码

@DaTa
public class Student {

private String name;

private List<String> books;

}

public class JSONMain {

public static void main(String[] args) {
    try {
        String str = "{\"name\": \"zhangsan\", \"books\" : \"西游记\"}";
        Student json1 = JSONObject.parseObject(str, Student.class);
        System.out.println("json1 = " + json1);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }

    String str2 = "{\"name\": \"zhangsan\", \"books\" : [\"西游记\", \"红楼梦\"]}";
    Student json2 = JSONObject.parseObject(str2, Student.class);
    System.out.println("json2 = " + json2);
}

}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

希望fastjson能支持解析单元素数组场景下不含有中括号,也能解析成功

相关日志输出

请复制并粘贴任何相关的日志输出。

com.alibaba.fastjson2.JSONException: json format error : "
at com.alibaba.fastjson2.reader.FieldReaderListStrMethod.readFieldValue(FieldReaderListStrMethod.java:83)
at com.alibaba.fastjson2.reader.ObjectReader2.readObject(ObjectReader2.java:241)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:258)
at com.alibaba.fastjson2.JSONObject.parseObject(JSONObject.java:1717)

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@freedomwolf008 freedomwolf008 added the bug Something isn't working label Jun 7, 2022
@wenshao wenshao added this to the 2.0.7 milestone Jun 7, 2022
@wenshao
Copy link
Member

wenshao commented Jun 7, 2022

@freedomwolf008
Copy link
Author

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.7-SNAPSHOT/ bug已修复,请帮用2.0.7-SNAPSHOT版本验证

非常感谢,再问一下,如果List books; 此处books为单个Book元素的List,这种场景顺便改了吗?
public class Book{
private String bookName;
private String version;
}

String str = "{"name": "zhangsan", "books" : { "bookName":"西游记", "version":"漫画版" }}";

@wenshao
Copy link
Member

wenshao commented Jun 8, 2022

@freedomwolf008 也支持了

@freedomwolf008
Copy link
Author

String str = "{"name": "zhangsan", "books" : { "bookName":"西游记", "version":"漫画版" }}";

非常感谢,两种场景测试,都通过了

@wenshao
Copy link
Member

wenshao commented Jun 11, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.7
问题已经修复,请用新版本

@wenshao wenshao closed this as completed Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants