alibaba / alibaba/fastjson2

[BUG]fastjson2 2.0.54序列化对象时,若对象的KEY是long类型 并且存在Filter时,Long类型的KEY多套了一层“”

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

### 问题描述
序列化对象时,若对象的KEY是long类型 并且存在Filter时,Long多套了一层“”

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

- OS信息: WIN 11
- JDK信息: Openjdk 17
- 版本信息:Fastjson2 2.0.54

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

```java
package com.demo.test;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.PropertyPreFilter;

import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) {
Map result = new HashMap();
result.put(1L, "2");
//{1:"2"}
System.out.println(JSON.toJSONString(result));
//{"1":"2"}
System.out.println(JSON.toJSONString(result, JSONWriter.Feature.WriteLongAsString));
//{"\"1\"":"2"}
System.out.println(JSON.toJSONString(result, new TestFilter(), JSONWriter.Feature.WriteLongAsString));
}

public static class TestFilter implements PropertyPreFilter {

@Override
public boolean process(JSONWriter jsonWriter, Object object, String name) {

return true;
}
}
}

```

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

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

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

Contributor guide

Open the contributing guide

Research direction

Start with the Test.main reproduction using JSON.toJSONString, JSONWriter.Feature.WriteLongAsString, and PropertyPreFilter. Trace the serialization path for Long map keys when the filter is present, then add a regression test covering the expected {"1":"2"} output and run the relevant Java test suite.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.