alibaba / alibaba/fastjson2

[BUG] WriteBigDecimalAsPlain不起作用

Open
#1,956 3 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

### 问题描述
Bean包含BigDecimal属性,通过JSON.toJSONString转为json字符串时,设置了WriteBigDecimalAsPlain,输出字符串中BigDecimal未转为字符串

### 重现步骤
测试代码:
```java
class TestData {
private BigDecimal amount;

public BigDecimal getAmount() {
return amount;
}

public TestData setAmount(BigDecimal amount) {
this.amount = amount;
return this;
}
}

@Test
public void testFastJson2() {
// Bean 中 BigDecimal 转字符串
TestData testData = new TestData().setAmount(new BigDecimal("1.50"));
System.out.println("结果1:" + JSON.toJSONString(testData, WriteBigDecimalAsPlain));

// JSONObject 中 BigDecimal 转字符串
JSONObject jsonObject1 = JSONObject.of("data", new BigDecimal("1.5011"));
System.out.println("结果2:" + jsonObject1.toJSONString(WriteBigDecimalAsPlain));

// JSONObject 中 BigDecimal 转字符串
JSONObject jsonObject2 = new JSONObject();
jsonObject1.put("data", new BigDecimal("1.5011"));
System.out.println("结果3:" + jsonObject1.toJSONString(WriteBigDecimalAsPlain));
}
```
输出结果:
```
结果1:{"amount":1.50}
结果2:{"data":1.5011}
结果3:{"data":1.5011}
```
### 期待的正确结果
```
结果1:{"amount":"1.50"}
结果2:{"data":"1.5011"}
结果3:{"data":"1.5011"}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the testFastJson2 example in the report and compare Bean serialization with JSONObject serialization. Trace the BigDecimal handling used by JSON.toJSONString and JSONObject.toJSONString. Done means WriteBigDecimalAsPlain produces quoted plain-value strings for all three examples, with regression coverage for the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.