mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add class
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
@@ -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);
|
||||
|
@@ -9,6 +9,7 @@ import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.test.bean.JSONBean;
|
||||
import cn.hutool.json.test.bean.ResultDto;
|
||||
@@ -27,9 +28,13 @@ import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* JSONObject单元测试
|
||||
@@ -425,6 +430,14 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals("{\"date\":[\"2020-06-05 11:16:11\"],\"bbb\":[\"222\"],\"aaa\":[\"123\"]}", json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTimestampTest(){
|
||||
String timeStr = "1970-01-01 00:00:00";
|
||||
final JSONObject jsonObject = JSONUtil.createObj().set("time", timeStr);
|
||||
final Timestamp time = jsonObject.get("time", Timestamp.class);
|
||||
Assert.assertEquals("1970-01-01 00:00:00.0", time.toString());
|
||||
}
|
||||
|
||||
public enum TestEnum {
|
||||
TYPE_A, TYPE_B
|
||||
}
|
||||
@@ -497,4 +510,14 @@ public class JSONObjectTest {
|
||||
return this.fieldToIgnore;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setEntryTest(){
|
||||
final HashMap<String, String> of = MapUtil.of("test", "testValue");
|
||||
final Set<Map.Entry<String, String>> entries = of.entrySet();
|
||||
final Map.Entry<String, String> next = entries.iterator().next();
|
||||
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(next);
|
||||
Console.log(jsonObject);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user