add class

This commit is contained in:
Looly
2020-10-18 00:12:50 +08:00
parent 741babe2c8
commit 65552f4f54
19 changed files with 398 additions and 59 deletions

View File

@@ -2,6 +2,7 @@ package cn.hutool.json;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TemporalAccessorUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
@@ -239,6 +240,9 @@ final class InternalJSONUtil {
*/
private static String formatDate(Object dateObj, String format) {
if (StrUtil.isNotBlank(format)) {
if(dateObj instanceof TemporalAccessor){
return TemporalAccessorUtil.format((TemporalAccessor) dateObj, format);
}
//用户定义了日期格式
return JSONUtil.quote(DateUtil.format(Convert.toDate(dateObj), format));
}

View File

@@ -351,6 +351,18 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
@Override
@Deprecated
public JSONObject put(String key, Object value) throws JSONException {
return set(key, value);
}
/**
* 设置键值对到JSONObject中在忽略null模式下如果值为<code>null</code>,将此键移除
*
* @param key 键
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
* @return this.
* @throws JSONException 值是无穷数字抛出此异常
*/
public JSONObject set(String key, Object value) throws JSONException {
if (null == key) {
return this;
}
@@ -366,19 +378,6 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
return this;
}
/**
* 设置键值对到JSONObject中在忽略null模式下如果值为<code>null</code>,将此键移除
*
* @param key 键
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
* @return this.
* @throws JSONException 值是无穷数字抛出此异常
*/
public JSONObject set(String key, Object value) throws JSONException {
put(key, value);
return this;
}
/**
* 一次性Put 键值对如果key已经存在抛出异常如果键值中有null值忽略
*
@@ -392,7 +391,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
if (rawHashMap.containsKey(key)) {
throw new JSONException("Duplicate key \"{}\"", key);
}
this.put(key, value);
this.set(key, value);
}
return this;
}
@@ -407,7 +406,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
*/
public JSONObject putOpt(String key, Object value) throws JSONException {
if (key != null && value != null) {
this.put(key, value);
this.set(key, value);
}
return this;
}
@@ -415,7 +414,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
@Override
public void putAll(Map<? extends String, ?> m) {
for (Entry<? extends String, ?> entry : m.entrySet()) {
this.put(entry.getKey(), entry.getValue());
this.set(entry.getKey(), entry.getValue());
}
}
@@ -432,7 +431,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
InternalJSONUtil.testValidity(value);
Object object = this.getObj(key);
if (object == null) {
this.put(key, value instanceof JSONArray ? new JSONArray(this.config).set(value) : value);
this.set(key, value instanceof JSONArray ? new JSONArray(this.config).set(value) : value);
} else if (object instanceof JSONArray) {
((JSONArray) object).set(value);
} else {
@@ -472,19 +471,19 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
public JSONObject increment(String key) throws JSONException {
Object value = this.getObj(key);
if (value == null) {
this.put(key, 1);
this.set(key, 1);
} else if (value instanceof BigInteger) {
this.put(key, ((BigInteger) value).add(BigInteger.ONE));
this.set(key, ((BigInteger) value).add(BigInteger.ONE));
} else if (value instanceof BigDecimal) {
this.put(key, ((BigDecimal) value).add(BigDecimal.ONE));
this.set(key, ((BigDecimal) value).add(BigDecimal.ONE));
} else if (value instanceof Integer) {
this.put(key, (Integer) value + 1);
this.set(key, (Integer) value + 1);
} else if (value instanceof Long) {
this.put(key, (Long) value + 1);
this.set(key, (Long) value + 1);
} else if (value instanceof Double) {
this.put(key, (Double) value + 1);
this.set(key, (Double) value + 1);
} else if (value instanceof Float) {
this.put(key, (Float) value + 1);
this.set(key, (Float) value + 1);
} else {
throw new JSONException("Unable to increment [" + JSONUtil.quote(key) + "].");
}
@@ -652,8 +651,11 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
} else if (source instanceof Map) {
// Map
for (final Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
this.put(Convert.toStr(e.getKey()), e.getValue());
this.set(Convert.toStr(e.getKey()), e.getValue());
}
}else if (source instanceof Map.Entry) {
final Map.Entry entry = (Map.Entry) source;
this.set(Convert.toStr(entry.getKey()), entry.getValue());
} else if (source instanceof CharSequence) {
// 可能为JSON字符串
init((CharSequence) source);