alibaba / alibaba/fastjson2

[QUESTION] fastjson1.x升级为fastjson2.x后,JSON.parseArray对错误的JSON字符串解析不抛异常了,如何兼容?

Open
#3,725 2 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 请描述您的问题
以下代码在fastjson1.x时会抛出异常,业务会捕捉异常进行降级处理,升级到2.x后,不抛异常了,导致分支代码没执行到。

``` java
//入参数据格式有两种: {"readNum":0,"userIds":[1,2,3]} 或者 [1,2,3]
//期望第一种数据过来的时候报错,业务会做特殊处理了
List userIds = new ArrayList<>();
try {
userIds = JSON.parseArray(event, Integer.class);
} catch (Exception e) {
//异常数据,执行降级处理逻辑。
}
```

fastjson2.x 执行 JSON.parseArray时会返回一个List,不会抛出异常。

是否可以增加一个选项,遇到反序列化类型不一致时抛出异常?

Contributor guide

Open the contributing guide

Research direction

Start at the JSON.parseArray(event, Integer.class) entry point shown in the report and compare its fastjson 1.x and 2.x behavior for an object input versus an array of integers. Check the existing parsing and deserialization options before deciding how a strict type-mismatch mode should be exposed. Done means the documented option preserves the expected exception-based fallback without changing default behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.