[BUG] json序列化有bug,key 序列化为 null
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
*简要描述您碰到的问题。*
key 序列化为 null
### 环境信息
*请填写以下信息:*
- OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.0.62](所有的版本都有这个问题)
com.alibaba.fastjson2
fastjson2
2.0.62
compile
### 重现步骤
*如何操作可以重现该问题:*
```java
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.filter.NameFilter;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class JsonTest2 {
private String buildJsonIndexSource(TestItem entity) throws Exception {
NameFilter nameFilter = (object, name, value) -> {
return null;
// return name;
};
return JSON.toJSONString(entity, nameFilter);
}
public static void main(String[] args) throws Exception {
TestItem document = new TestItem();
document.setRequestId("123456");
document.setMeta(new HashMap() {{
put("aaaa", "美味烧鸡.png");
put("bbbb", "烧烤");
}});
JsonTest2 jsonTest = new JsonTest2();
String json = jsonTest.buildJsonIndexSource(document);
System.out.println(json);
}
@Data
public static class TestItem implements Serializable {
private Map meta;
private String requestId;
}
}
```
### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
{"meta":{"aaaa":"美味烧鸡.png","bbbb":"烧烤"},"requestId":"123456"}
### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
{"meta":{null:"美味烧鸡.png",null:"烧烤"},"requestId":"123456"}
#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
Contributor guide
Research direction
Run the JsonTest2 example using JSON.toJSONString with the NameFilter that returns null, and compare the observed map keys with the expected JSON. Trace how NameFilter is applied to Map keys and add regression coverage showing that the expected keys remain intact while values are serialized correctly.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100