[BUG] BigDecimal with @JsonFormat(shape = STRING) produces invalid JSON since 2.0.63
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
BigDecimal 字段使用 Jackson 注解:
```java
@JsonFormat(shape = JsonFormat.Shape.STRING)
private BigDecimal amount
```
fastjson2 2.0.61 及 2.0.63 会生成非法 JSON:
```
{"amount":string200}
```
预期结果应为:
```
{"amount":"200"}
```
### 最小复现代码
### 环境信息
*请填写以下信息:*
OS信息: Windows 11 10.0 8Core amd64 64bit
JDK信息: Oracle Corporation 21.0.11
版本信息:2.0.63 和 1.8都测试了
### 重现步骤
```java
package org.example;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.math.BigDecimal;
import java.util.Date;
public class TestJson {
private String goodsUnit;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private BigDecimal quantity;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date invoiceDate;
public String getGoodsUnit() {
return goodsUnit;
}
public void setGoodsUnit(String goodsUnit) {
this.goodsUnit = goodsUnit;
}
public BigDecimal getQuantity() {
return quantity;
}
public void setQuantity(BigDecimal quantity) {
this.quantity = quantity;
}
public Date getInvoiceDate() {
return invoiceDate;
}
public void setInvoiceDate(Date invoiceDate) {
this.invoiceDate = invoiceDate;
}
public static void main(String[] args) throws Exception {
System.out.println(com.alibaba.fastjson.JSON.VERSION);
System.out.println(com.alibaba.fastjson2.JSON.VERSION);
String req = "{\"goodsUnit\":\"adfa\",\"quantity\":\"6.56000000001\",\"invoiceDate\":\"2026-07-30 10:57:08\"}";
TestJson testJson1 = com.alibaba.fastjson.JSONObject.parseObject(req,
TestJson.class);
System.out.println(com.alibaba.fastjson.JSON.toJSONString(testJson1));
TestJson testJson2 = com.alibaba.fastjson2.JSONObject.parseObject(req,
TestJson.class);
System.out.println(com.alibaba.fastjson2.JSON.toJSONString(testJson2));
}
}
```
1. 执行程序
```
2.0.61
2.0.61
{"goodsUnit":"adfa","invoiceDate":"2026-07-30","quantity":string7}
{"goodsUnit":"adfa","invoiceDate":"2026-07-30","quantity":string7}
```
### 期待的正确结果
期待返回
{"goodsUnit":"adfa","invoiceDate":"2026-07-30","quantity":“6.56000000001”}
@JsonFormat只为了在controller响应序列化使用,跟你内部使用无关
### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
#### 附加信息
```
com.alibaba
fastjson
2.0.63
com.alibaba.fastjson2
fastjson2
2.0.63
com.fasterxml.jackson.core
jackson-databind
2.13.5
```
Contributor guide
Research direction
Start with the TestJson.main reproduction shown in the issue and compare fastjson2 serialization of the BigDecimal field annotated with @JsonFormat(shape = STRING). Verify that the output contains a quoted decimal value rather than string7 or string200, and add a regression test covering this case.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100