alibaba / alibaba/fastjson2

[BUG]使用fastjson2(2.0.47)序列化后json串出现了两个@type 导致fastjson1(1.2.83)版本反序列化丢失属性

Open
#2,374 1 comment 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

### 问题描述
*使用fastjson2(2.0.47)序列化后json串出现了两个@type 导致fastjson1(1.2.83)版本反序列化丢失属性*
![image](https://github.com/alibaba/fastjson2/assets/13016249/fde163ad-0582-4d94-aa9b-da429b19f746)

### 环境信息
*请填写以下信息:*

- OS信息: [e.g.:Mac os 14.2.1]
- JDK信息: [e.g.:jdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.0.47]

### 重现步骤
*如何操作可以重现该问题:*
1、使用fastjson2(2.0.47)序列化后json串出现了两个@type 导致fastjson1(1.2.83)版本反序列化丢失属性
2、反序列化后map接口中只有一个属性“xxx”

```java
@Test
public void test() {
List result = new ArrayList<>();
A a = new A();
a.setA("a");
Map m = new HashMap<>();
m.put("k1", "v1");
m.put("k2", "v2");
m.put("k3", "v3");
m.put("k4", "v4");
m.put("k5", "v5");
a.setM(m);
result.add(a);
String json = JSON.toJSONString(result, SerializerFeature.WriteClassName);

List as = JSON.parseArray(json, A.class);
String json2 = JSON.toJSONString(as, SerializerFeature.WriteClassName);
// 实际有2个, fastjson1 序列化时,只会有一个。
Assert.assertEquals(1, StringUtils.countMatches(json2, "java.util.HashMap"));
}

@Data
public static class A {
private String a;
private Map m;
}
```

### 期待的正确结果
正确序列化

### 相关日志输出
无报错

Contributor guide

Open the contributing guide

Research direction

Start with the supplied Java test and inspect the WriteClassName serialization and parseArray round trip across fastjson2 2.0.47 and fastjson1 1.2.83. Compare the generated JSON and the deserialized map contents; done means the round trip preserves all map properties and emits only one java.util.HashMap type marker.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.