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] 字符串与List类型不匹配时转换异常 #2440

Closed
DamonBao opened this issue Apr 12, 2024 · 2 comments
Closed

[BUG] 字符串与List类型不匹配时转换异常 #2440

DamonBao opened this issue Apr 12, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@DamonBao
Copy link

DamonBao commented Apr 12, 2024

问题描述

类型不匹配时转换异常

@Test
public void testJson() {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("lines", "1,2");
    Sample sample = jsonObject.toJavaObject(Sample.class);
    System.out.println(sample);
}

static class Sample {

    private List<String> lines;

    public List<String> getLines() {
        return lines;
    }

    public void setLines(List<String> lines) {
        this.lines = lines;
    }
}

环境信息

请填写以下信息:

  • OS信息: macos / windows11
  • JDK信息: jdk8 jdk17
  • 版本信息:Fastjson2 2.0.48 1.x兼容包 2.0.48

重现步骤

如何操作可以重现该问题:

以上代码报错

期待的正确结果

如果类型不匹配,还是能够转换成功,值可以为null,避免报错。
fastjson 1.2.83目前是可以转换成功,lines[0]值为"1,2"

相关日志输出

com.alibaba.fastjson2.JSONException: offset 1, character 1, line 1, column 1, fastjson-version 2.0.48 1,2

at com.alibaba.fastjson2.reader.ObjectReaderImplList.readObject(ObjectReaderImplList.java:589)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:786)
at com.alibaba.fastjson2.function.impl.StringToAny.apply(StringToAny.java:81)
at com.alibaba.fastjson2.reader.FieldReader.acceptAny(FieldReader.java:425)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.createInstance(ObjectReaderAdapter.java:659)
at com.alibaba.fastjson.JSONObject.toJavaObject(JSONObject.java:1011)
@DamonBao DamonBao added the bug Something isn't working label Apr 12, 2024
@wenshao wenshao added this to the 2.0.50 milestone Apr 13, 2024
@wenshao
Copy link
Member

wenshao commented May 3, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.50-SNAPSHOT/
问题已修复,请帮忙用2.0.50-SNAPSHOT版本验证,2.0.50版本预计在5月12日前发布

@wenshao wenshao added the fixed label May 3, 2024
@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50已发布,请用新版本

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

No branches or pull requests

2 participants