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] parsing ['1'] , ["1.1"] #3276

Closed
Cooper-Zhong opened this issue Jan 14, 2025 · 4 comments
Closed

[BUG] parsing ['1'] , ["1.1"] #3276

Cooper-Zhong opened this issue Jan 14, 2025 · 4 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@Cooper-Zhong
Copy link
Contributor

问题描述

Integer和Long类型的 ["123"] 可以解析,Double无法解析,Character会只保留[

环境信息

  • OS信息: [e.g.:MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [e.g.:Openjdk 17.0.6]
  • 版本信息:[e.g.:Fastjson2 2.0.54]
    @Test
    public void jsonSerializationTest3() {
        String json = "{\n" +
                "    \"c\":[\"1.1\"]\n" +
                "}";

        Bean bean = JSON.parseObject(json, Bean.class);
        // com.alibaba.fastjson2.JSONException: can not cast to decimal
        System.out.println(bean.getC());
    }
    
    @Test
    public void jsonSerializationTest2() {
        String json = "{\n" +
                "    \"b\":['1']\n" +
                "}";
        Bean bean = JSON.parseObject(json, Bean.class);
        System.out.println(bean.getB()); // [
    }
    @Data
    public static class Bean {
        private Character b;
        private Double c;
    }
@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label Jan 14, 2025
@wenshao wenshao added the fixed label Feb 9, 2025
@wenshao wenshao added this to the 2.0.55 milestone Feb 9, 2025
@wenshao wenshao modified the milestones: 2.0.55, 2.0.56 Feb 22, 2025
@wenshao
Copy link
Member

wenshao commented Feb 22, 2025

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

@wenshao wenshao closed this as completed Feb 22, 2025
@yhzdys
Copy link

yhzdys commented Feb 25, 2025

@wenshao 由于这次更新改变了对单元素数组的默认操作逻辑,造成了一些线上事故,目前暂时通过降低版本解决了,新版本可以通过设置Feature来恢复老版本的运行逻辑吗

@wenshao
Copy link
Member

wenshao commented Feb 25, 2025

为什么会导致故障,可以详细说下么?

@yhzdys
Copy link

yhzdys commented Feb 26, 2025

为什么会导致故障,可以详细说下么?

@wenshao
我这边的系统有一个可以自定义执行各种业务逻辑的功能,业务逻辑的输入参数可以自定义
比如说:参数名=usernames,参数的数据类型=StringList
前端会根据参数的类型给到后端,后端先统一按字符串处理,再根据事先定义的 参数数据类型 去解析参数

简化后的业务代码如下

public class Test {

    @Data
    public static class Request { // controller接收请求body的类
        private Map<String, String> variables;
    }

    public static void main(String[] args) throws Throwable {
        // 系统配置的变量名对应的数据类型
        final Map<String, String> VariableTypeMapping = new LinkedHashMap<>(2, 1.0F, false);
        VariableTypeMapping.put("usernames", "StringList"); // 变量‘usernames’的数据类型是List<String>

        // 前端调后端接口传入的数据
        String apiInput = """
                {
                    "variables": {
                        "usernames": ["张三"]
                    }
                }
                """;
        // 后端将前端请求的数据解析为Java对象
        Request request = JSON.parseObject(apiInput, Request.class);
        for (Map.Entry<String, String> entry : request.variables.entrySet()) {
            // 根据变量名“usernames”获取这个变量的数据类型
            String dataType = VariableTypeMapping.get(entry.getKey());
            if ("StringList".equals(dataType)) {
                JSON.parseArray(entry.getValue()); // 2.0.56 版本,这里获取到的数据是‘张三’,而老版本的fastsjon2获取到的是‘["张三"]’
            }
        }
    }
}

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

3 participants