Skip to content

[QUESTION] JSON.parseObject(String)在fastjson1里只会抛JSONException,但是fastjson2里没有做异常类型的收敛,会抛出ArrayIndexOutOfBoundsException #3610

@doom20082004

Description

@doom20082004

您好,

我在使用fastjson2.0.57版本解析被截断的JSON字符串时抛出ArrayIndexOutOfBoundsException

    String str = "{\"screenWidth\":1920,\"isSupportSmallWindow\"";
    JSONObject a = JSON.parseObject(str);

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 42 out of bounds for length 42
at com.alibaba.fastjson2.JSONReaderASCII.readFieldName(JSONReaderASCII.java:1058)
at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:2152)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:348)
at Main.main(Main.java:11)

但是在fastjson1.2.73版本的源码JSON.parseObject()里,有把其他异常统一转成JSONException的逻辑

public static JSONObject parseObject(String text) {
    Object obj = parse(text);
    if (obj instanceof JSONObject) {
        return (JSONObject) obj;
    }

    try {
        return (JSONObject) JSON.toJSON(obj);
    } catch (RuntimeException e) {
        throw new JSONException("can not cast to JSONObject.", e);
    }
}

为啥fastjson2里不把其他异常转成JSONException,使得项目里升级fastjson2后,原有捕获JSONException的地方,还得增加捕获其他异常。

盼望解惑,谢谢大神。

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions