[QUESTION]实现ObjectWriter接口配合@JSONField(serializeUsing = xxx.class)未生效
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 请描述您的问题
*询问有关本项目的使用和其他方面的相关问题。*
**[依赖]**
com.alibaba.fastjson2
fastjson2
2.0.43
com.alibaba.fastjson2
fastjson2-extension
2.0.43
com.alibaba.fastjson2
fastjson2-extension-spring5
2.0.43
com.alibaba.fastjson2
fastjson2-extension-spring6
2.0.43
**[配置]**
fastJsonConfig.setWriterFeatures(
//输出list 为 []
JSONWriter.Feature.WriteNullListAsEmpty,
//json格式化
JSONWriter.Feature.PrettyFormat,
//输出map中value为null的数据
JSONWriter.Feature.WriteMapNullValue,
//输出boolean 为 false
JSONWriter.Feature.WriteNullBooleanAsFalse,
//输出list 为 []
JSONWriter.Feature.WriteNullListAsEmpty,
//输出number 为 0
JSONWriter.Feature.WriteNullNumberAsZero,
//输出字符串 为 ""
JSONWriter.Feature.WriteNullStringAsEmpty,
//对map进行排序
JSONWriter.Feature.MapSortField);
**[自定义序列化]**
@Override
public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
System.out.println("自定义序列化规则=======================");
Optional
.ofNullable(object).
filter(Integer.class::isInstance)
.map(Integer.class::cast)
.ifPresent(jsonWriter::writeInt32);
}
**[问题]**
项目中的全局配置Integer类型默认值为0,但是我的业务中对于特定的类需要的默认值为null,想通过实现ObjectWriter接口配合@JSONField(serializeUsing = xxx.class)达到目的却未能实现,是我使用的不对吗?
Contributor guide
Research direction
Start by reproducing the behavior with fastjson2 2.0.43, the shown writer features, and a field using @JSONField(serializeUsing = ...). Trace how ObjectWriter and the annotation are applied during serialization; done means the custom writer controls the targeted Integer field without changing the global null-number behavior.
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
- 35/100