add config

This commit is contained in:
Looly 2021-03-26 20:36:26 +08:00
parent fc4a644e35
commit 8b3155c556
4 changed files with 46 additions and 2 deletions

View File

@ -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 】 增加DesensitizedUtilpr#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

View File

@ -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));

View File

@ -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;
}
}

View File

@ -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() {
// 测试转义