alibaba / alibaba/fastjson2

[BUG]默认策略下,序列化后的对象,反序列化不回来

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

### 问题描述
*简要描述您碰到的问题。*
默认策略下,序列化后的对象,反序列化不回来

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class PortKeyVO {}

String testStr = "{{\"neId\":167773037,\"portId\":14,\"shelfId\":0,\"slotId\":1,\"subSlotId\":-1}:\"4810\",{\"neId\":167772905,\"portId\":4,\"shelfId\":0,\"slotId\":1,\"subSlotId\":-1}:\"4810\"}";
ParserConfig config = new ParserConfig();
config.setAutoTypeSupport(true);
Type type = new TypeReference>(){}.getType();
Map keyVO = JSON.parseObject(testStr, type, config);
System.out.println(com.alibaba.fastjson2.JSON.toJSONString(keyVO, JSONWriter.Feature.WriteClassName));
System.out.println(JSON.toJSONString(keyVO));

String json2Str = "{\"PortKeyVO(neId=167773037, shelfId=0, slotId=1, subSlotId=-1, portId=14)\":\"4810\",\"PortKeyVO(neId=167772905, shelfId=0, slotId=1, subSlotId=-1, portId=4)\":\"4810\"}";
Map keyVO2 = com.alibaba.fastjson2.JSON.parseObject(json2Str, type);

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

- OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.x.x]

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

1. 使用 `xxx.xxx` 方法
2. 输入 `...` 数据
3. 出现 `...` 错误
```java
//可在此输入示例代码
```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
期待能正常反序列化成正确的map

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
{"PortKeyVO(neId=167773037, shelfId=0, slotId=1, subSlotId=-1, portId=14)":"4810","PortKeyVO(neId=167772905, shelfId=0, slotId=1, subSlotId=-1, portId=4)":"4810"}
{{"empty":false,"neId":167773037,"portId":14,"shelfId":0,"slotId":1,"subSlotId":-1}:"4810",{"empty":false,"neId":167772905,"portId":4,"shelfId":0,"slotId":1,"subSlotId":-1}:"4810"}
Exception in thread "main" com.alibaba.fastjson2.JSONException: expect ':', but , offset 1, character ", line 1, column 1, fastjson-version 2.0.52 "PortKeyVO(neId=167773037, shelfId=0, slotId=1, subSlotId=-1, portId=14)"
at com.alibaba.fastjson2.JSONReaderUTF16.readFieldNameHashCode(JSONReaderUTF16.java:1374)
at com.alibaba.fastjson2.reader.ORG_1_5_PortKeyVO.readObject(Unknown Source)
at com.alibaba.fastjson2.util.TypeUtils.cast(TypeUtils.java:1510)
at com.alibaba.fastjson2.util.TypeUtils.cast(TypeUtils.java:1409)
at com.alibaba.fastjson2.util.TypeUtils.cast(TypeUtils.java:1404)
at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:356)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1305)

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

fastjson 1.2.X 实现:
{
List propertyFilters = this.propertyFilters;
if (propertyFilters != null && propertyFilters.size() > 0) {
if (entryKey == null || entryKey instanceof String) {
if (!this.apply(serializer, object, (String) entryKey, value)) {
continue;
}
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey);
if (!this.apply(serializer, object, strKey, value)) {
continue;
}
}
}
}

fastjson2.0.52 实现:
for (Map.Entry entry : (Iterable) map.entrySet()) {
Object value = entry.getValue();
if (value == null && !writeNulls) {
continue;
}

Object entryKey = entry.getKey();
String key;
if (entryKey == null) {
key = null;
} else {
key = entryKey.toString();
}

如果我们的Map的key对象没有重写tostring方法,直接无法序列化key
{"vo.port.PortKeyVO@e06f294":"4810","vo.port.PortKeyVO@aeb0a446":"4810"}

Contributor guide

Open the contributing guide

Research direction

Reproduce the shown PortKeyVO map round trip with JSON.parseObject and JSON.toJSONString, comparing the fastjson 1.x behavior included in the issue. Inspect map-key handling at ObjectReaderImplMapTyped.readObject and the serialization entry point, then verify that a map with object keys serializes and deserializes back to the expected keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, json
Domain
backend
Issue type
Bug
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.