[BUG]JSONWriter.Feature.BrowserCompatible和Feature.WriteBigDecimalAsPlain是否有冲突,针对BigDecimal和BigInteger类型与预期不符
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
*简要描述您碰到的问题。*
问题1:设置JSONWriter.Feature.BrowserCompatible,接口返回BigDecimal和BigInteger类型均为字符串
问题2:不设置JSONWriter.Feature.BrowserCompatible,接口返回BigDecimal和BigInteger类型均为数字类型
问题3:不设置JSONWriter.Feature.BrowserCompatible且设置Feature.WriteBigDecimalAsPlain,接口返回BigDecimal和BigInteger类型均为数字类型,感觉Feature.WriteBigDecimalAsPlain不生效
问题4:设置JSONWriter.Feature.BrowserCompatible且设置Feature.WriteBigDecimalAsPlain,接口返回BigDecimal和BigInteger类型均为字符串
问题总结:有JSONWriter.Feature.BrowserCompatible特性,BigDecimal和BigInteger类型数据均反序列化为字符串;设置Feature.WriteBigDecimalAsPlain不生效,无法序列化BigDecimal和BigInteger类型数据为字符串
### 环境信息
*请填写以下信息:*
- 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.52]
### 重现步骤
*如何操作可以重现该问题:*
1. 使用 `xxx.xxx` 方法
2. 输入 `...` 数据
3. 出现 `...` 错误
Controller类示例
```java
@GetMapping(value = "/mockData")
public AjaxResult.Result mockData(@RequestParam("mock") Object mock){
JSONObject result = new JSONObject();
result.put("boolean", Boolean.TRUE);
result.put("byte", Byte.MAX_VALUE);
result.put("char", Character.MAX_VALUE);
result.put("int", Integer.MAX_VALUE);
result.put("long", Long.MAX_VALUE);
result.put("float", Float.MAX_VALUE);
result.put("double", Double.MAX_VALUE);
result.put("datetime", LocalDateTime.now());
result.put("date", LocalDate.now());
result.put("time", LocalTime.now());
result.put("timestamp", System.currentTimeMillis());
result.put("nanotime", System.nanoTime());
result.put("BigDecimal", new BigDecimal("12345678901234567890"));
// result.put("BigReal", new BigReal("999999999"));
result.put("BigInteger", BigInteger.valueOf(Long.MIN_VALUE));
result.put("string", "为了适应行业用户数字化升级的新需求和保持产品技术的竞争力");
result.put("round", new Random().nextLong());
result.put("mock", mock);
return success(ReturnInfo.OPERATION_SUCCESS_MSG, result);
}
```
MVC配置示例
```java
@Configuration
public class FastjsonGlobalConfigurer implements WebMvcConfigurer {
/**
* Fastjson默认写特性
*/
public static final JSONWriter.Feature[] FASTJSON_DEFAULT_WRITER_FEATURES = {JSONWriter.Feature.BrowserCompatible, JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.WriteNullListAsEmpty, JSONWriter.Feature.WriteNullBooleanAsFalse, JSONWriter.Feature.WriteNulls,JSONWriter.Feature.WriteEnumsUsingName,JSONWriter.Feature.WritePairAsJavaBean,JSONWriter.Feature.IgnoreErrorGetter,JSONWriter.Feature.LargeObject};
/**
* Fastjson默认读特性
*/
public static final JSONReader.Feature[] FASTJSON_DEFAULT_READER_FEATURES = {JSONReader.Feature.SupportSmartMatch, JSONReader.Feature.SupportArrayToBean, JSONReader.Feature.UseNativeObject, JSONReader.Feature.SupportClassForName,JSONReader.Feature.IgnoreSetNullValue,JSONReader.Feature.AllowUnQuotedFieldNames,JSONReader.Feature.IgnoreCheckClose,JSONReader.Feature.IgnoreAutoTypeNotMatch /* ,JSONReader.Feature.NonErrorOnNumberOverflow */ };
private FastJsonConfig defaultFastJsonConfig() {
Charset charset = StandardCharsets.UTF_8;
JSONWriter.Feature[] writerFeatures = FASTJSON_DEFAULT_WRITER_FEATURES;
JSONReader.Feature[] readerFeatures = FASTJSON_DEFAULT_READER_FEATURES;
// 创建配置类
FastJsonConfig config = new FastJsonConfig();
config.setWriterFeatures(writerFeatures);
config.setReaderFeatures(readerFeatures);
config.setCharset(charset);
config.setDateFormat(DateUtils.DEFAULT_FORMAT_DATE_TIME);
return config;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
FastJsonJsonView view = new FastJsonJsonView();
view.setFastJsonConfig(defaultFastJsonConfig());
registry.enableContentNegotiation(view);
}
@Override
public void extendMessageConverters(List> converters) {
Charset charset = StandardCharsets.UTF_8;
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFastJsonConfig(defaultFastJsonConfig());
converter.setDefaultCharset(charset);
List supportedMediaTypes = Lists.newArrayList();
supportedMediaTypes.add(MediaType.ALL);
converter.setSupportedMediaTypes(supportedMediaTypes);
converters.add(1, converter);
oconverter.setSupportedMediaTypes(supportedMediaTypes);
oconverter.setDefaultCharset(charset);
}
}
```
### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
Contributor guide
Research direction
Start with the provided controller and MVC FastJsonConfig, focusing on JSONWriter.Feature.BrowserCompatible and WriteBigDecimalAsPlain. Reproduce the four BigDecimal and BigInteger serialization combinations, then inspect the relevant JSONWriter behavior and existing tests. Done means the interaction is clarified and covered by a regression test with the expected output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100