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

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