alibaba / alibaba/fastjson2

[BUG] 反序列化对于字符串[]逻辑不一致

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

Description

### 问题描述
对于字符串里本身就是有[],如果里面就1份数据,转回来会丢失[]。如果里面多份数据,则会保留[]

### 环境信息
- JDK信息: OpenJdk21
- 版本信息:2.0.61

### 重现步骤
*如何操作可以重现该问题:*

```java
@Getter
@Setter
class TestString {
String apiAuth;
}

public static void main(String[] args) {
String text1 = "{\"apiAuth\":[\"权限1\",\"权限2\"]}";
TestString testString = JSON.parseObject(text1, TestString.class);
System.out.println(JSON.toJSONString(testString));

String text2 = "{\"apiAuth\":[\"权限1\"]}";
TestString testString2 = JSON.parseObject(text2, TestString.class);
System.out.println(JSON.toJSONString(testString2));

String text3 = "{\"apiAuth\":[\"权限1\"]}";
String s3 = JSON.parseObject(text3, String.class);
System.out.println(s3);
}
```

### 期待的正确结果
*即使[]里一份数据,反序列化回来的时候继续保持*

### 相关日志输出
如果不是反序列化对象则会保留,请看s3的输出,上面三个输出如下:
{"apiAuth":"[\"权限1\",\"权限2\"]"}
{"apiAuth":"权限1"} // 这个不对,自动给我丢了[]
{"apiAuth":["权限1"]}

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the three examples with OpenJDK 21 and fastjson2 2.0.61, focusing on JSON.parseObject and JSON.toJSONString. Trace the deserialization of a JSON array into the String apiAuth field and compare it with parsing directly to String. Done means a one-item array remains enclosed in [] after round-tripping, while the existing multi-item and direct-String behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.