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

IntegerCodec#deserialze中的TypeUtils.castToInt 在value是逗号分隔字符串时,会把逗号去除转成Integer,应该直接报转换异常。 版本:1.2.67 #4497

Open
Chris1417 opened this issue Apr 12, 2024 · 0 comments

Comments

@Chris1417
Copy link

public static Integer castToInt(Object value){
if(value == null){
return null;
}

    if(value instanceof Integer){
        return (Integer) value;
    }

    if(value instanceof BigDecimal){
        return intValue((BigDecimal) value);
    }

    if(value instanceof Number){
        return ((Number) value).intValue();
    }

    if(value instanceof String){
        String strVal = (String) value;
        if(strVal.length() == 0 //
                || "null".equals(strVal) //
                || "NULL".equals(strVal)){
            return null;
        }
        if(strVal.indexOf(',') != -1){
            strVal = strVal.replaceAll(",", ""); //  此处删除了逗号
        }
        
        Matcher matcher = NUMBER_WITH_TRAILING_ZEROS_PATTERN.matcher(strVal);
        if(matcher.find()) {
            strVal = matcher.replaceAll("");
        }
        return Integer.parseInt(strVal);
    }

    if(value instanceof Boolean){
        return (Boolean) value ? 1 : 0;
    }
    if(value instanceof Map){
        Map map = (Map) value;
        if(map.size() == 2
                && map.containsKey("andIncrement")
                && map.containsKey("andDecrement")){
            Iterator iter = map.values().iterator();
            iter.next();
            Object value2 = iter.next();
            return castToInt(value2);
        }
    }
    throw new JSONException("can not cast to int, value : " + value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant