alibaba / alibaba/fastjson2

[BUG] java.lang.ClassCastException: com.alibaba.fastjson2.JSONObject cannot be cast to com.alibaba.fastjson.JSONObject

Open
#2,903 0 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

### 问题描述
#Fastjson v1兼容模块,遇到多层未指定类型的容器类,仍然会出现parseObject和toJavaObject类型不一致的问题。 https://github.com/alibaba/fastjson2/issues/2673

### 环境信息

- OS信息: win10
- JDK信息: Openjdk 1.8
- 版本信息:
```xml

com.alibaba
fastjson
2.0.52

```

### 重现步骤
```java
@Test
public void fastJosn2() {
String jsonStr = "{\"children\":[{\"children\":[{\"name\":\"child-2\"}],\"name\":\"child-1\"}],\"name\":\"root\"}";
Catalog root = JSON.parseObject(jsonStr, Catalog.class);
List children = root.getChildren();
Catalog child1 = JSON.toJavaObject(children.get(0), Catalog.class);
Assert.assertEquals(root.getChildren().get(0).getClass(), child1.getChildren().get(0).getClass());
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Catalog {
private String name;
private List children;
}
```

```
java.lang.AssertionError:
Expected :class com.alibaba.fastjson.JSONObject
Actual :class com.alibaba.fastjson2.JSONObject

```

### 期待的正确结果
兼容模块下parseObject和toJavaObject出来的结果都是com.alibaba.fastjson.JSONObject。

Contributor guide

Open the contributing guide

Research direction

Start with the Fastjson v1 compatibility module and the reproduced test using JSON.parseObject and JSON.toJavaObject on the nested Catalog example. Check how the two conversion paths create nested children, then verify that both produce com.alibaba.fastjson.JSONObject for the expected assertion.

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.