alibaba / alibaba/fastjson2

[BUG] 使用JSON.parseArray 反序列化非数组json("{....}"), 不论传入Class是什么类型,均转换为List<String> 内部String元素为该json字符串

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

Description

### 问题描述
使用JSON.parseArray 反序列化非数组json("{....}"), 不论传入Class是什么类型,均转换为List 内部String元素为改json字符串
涉及api:
com.alibaba.fastjson2.JSON#parseArray(java.lang.String, java.lang.Class)

### 环境信息
- OS信息: Win11
- JDK信息: 1.8
- 版本信息:Fastjson2 2.0.53

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

```java
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.assertj.core.api.Assertions;
import org.junit.Test;

public class JsonArrayParseTest {

@Test
public void name() {
String json = "{\"scene\":\"abc\",\"expireTime\":2880}";
List ls = com.alibaba.fastjson2.JSON.parseArray(json, Bean.class);
System.out.println(ls.stream().map(Object::getClass).collect(Collectors.toList()));
System.out.println(ls.stream().map(Objects::toString).collect(Collectors.toList()));

Assertions.assertThatThrownBy(() -> com.alibaba.fastjson.JSON.parseArray(json, Bean.class));
}

private static class Bean {

private String scene;

private int expireTime;

public String getScene() {
return scene;
}

public void setScene(String scene) {
this.scene = scene;
}

public int getExpireTime() {
return expireTime;
}

public void setExpireTime(int expireTime) {
this.expireTime = expireTime;
}
}
}
```

### 期待的正确结果
如FastJson1那样报错。

Contributor guide

Open the contributing guide

Research direction

Start with the JSON.parseArray(String, Class) API and the JsonArrayParseTest reproduction shown in the issue. Run the example with a non-array object and compare fastjson2 with fastjson 1 behavior. Done means passing Bean.class to parseArray with object JSON reports an error instead of returning a List containing the JSON string.

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
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.