mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add config
This commit is contained in:
parent
fc4a644e35
commit
8b3155c556
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.6.2 (2021-03-25)
|
||||
# 5.6.2 (2021-03-26)
|
||||
|
||||
### 新特性
|
||||
* 【core 】 Validator增加车架号(车辆识别码)验证、驾驶证(驾驶证档案编号)的正则校验(pr#280@Gitee)
|
||||
@ -12,6 +12,7 @@
|
||||
* 【core 】 增加DesensitizedUtil(pr#282@Gitee)
|
||||
* 【core 】 增加DateTime字符串构造(issue#I3CQZG@Gitee)
|
||||
* 【core 】 修改ArrayUtil代码风格(pr#287@Gitee)
|
||||
* 【json 】 JSONConfig增加setStripTrailingZeros配置(issue#I3DJI8@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【core 】 修复FileTypeUtil中OFD格式判断问题(pr#1489@Github)
|
||||
|
@ -50,7 +50,9 @@ final class InternalJSONUtil {
|
||||
} else if (value instanceof Iterable || value instanceof Iterator || value.getClass().isArray()) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
writer.write(NumberUtil.toStr((Number) value));
|
||||
// since 5.6.2可配置是否去除末尾多余0,例如如果为true,5.0返回5
|
||||
final boolean isStripTrailingZeros = null == config || config.isStripTrailingZeros();
|
||||
writer.write(NumberUtil.toStr((Number) value, isStripTrailingZeros));
|
||||
} else if (value instanceof Date || value instanceof Calendar || value instanceof TemporalAccessor) {
|
||||
final String format = (null == config) ? null : config.getDateFormat();
|
||||
writer.write(formatDate(value, format));
|
||||
|
@ -36,6 +36,11 @@ public class JSONConfig implements Serializable {
|
||||
*/
|
||||
private boolean transientSupport = true;
|
||||
|
||||
/**
|
||||
* 是否去除末尾多余0,例如如果为true,5.0返回5
|
||||
*/
|
||||
private boolean stripTrailingZeros = true;
|
||||
|
||||
/**
|
||||
* 创建默认的配置项
|
||||
*
|
||||
@ -192,4 +197,23 @@ public class JSONConfig implements Serializable {
|
||||
this.transientSupport = transientSupport;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否去除末尾多余0,例如如果为true,5.0返回5
|
||||
* @return 是否去除末尾多余0,例如如果为true,5.0返回5
|
||||
* @since 5.6.2
|
||||
*/
|
||||
public boolean isStripTrailingZeros() {
|
||||
return stripTrailingZeros;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否去除末尾多余0,例如如果为true,5.0返回5
|
||||
* @param stripTrailingZeros 是否去除末尾多余0,例如如果为true,5.0返回5
|
||||
* @since 5.6.2
|
||||
*/
|
||||
public JSONConfig setStripTrailingZeros(boolean stripTrailingZeros) {
|
||||
this.stripTrailingZeros = stripTrailingZeros;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -173,6 +173,23 @@ public class JSONUtilTest {
|
||||
Assert.assertEquals("{\"test2\":12.0}", jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setStripTrailingZerosTest() {
|
||||
// 默认去除多余的0
|
||||
final JSONObject jsonObjectDefault = JSONUtil.createObj()
|
||||
.set("test2", 12.00D);
|
||||
Assert.assertEquals("{\"test2\":12}", jsonObjectDefault.toString());
|
||||
|
||||
// 不去除多余的0
|
||||
final JSONObject jsonObject = JSONUtil.createObj(JSONConfig.create().setStripTrailingZeros(false))
|
||||
.set("test2", 12.00D);
|
||||
Assert.assertEquals("{\"test2\":12.0}", jsonObject.toString());
|
||||
|
||||
// 去除多余的0
|
||||
jsonObject.getConfig().setStripTrailingZeros(true);
|
||||
Assert.assertEquals("{\"test2\":12}", jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseObjTest() {
|
||||
// 测试转义
|
||||
|
Loading…
x
Reference in New Issue
Block a user