This commit is contained in:
Looly
2020-04-11 10:38:07 +08:00
parent 47be0f4f79
commit 73fd3b849f
21 changed files with 128 additions and 122 deletions

View File

@@ -3,6 +3,7 @@ package cn.hutool.json;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.collection.ArrayIter;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
@@ -41,7 +42,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
/** 持有原始数据的List */
private final List<Object> rawList;
/** 配置项 */
private JSONConfig config;
private final JSONConfig config;
// -------------------------------------------------------------------------------------------------------------------- Constructor start
/**
@@ -365,14 +366,13 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
return rawList.contains(o);
}
@SuppressWarnings("NullableProblems")
@Override
public Object[] toArray() {
return rawList.toArray();
}
@Override
@SuppressWarnings({"unchecked", "NullableProblems"})
@SuppressWarnings({"unchecked"})
public <T> T[] toArray(T[] a) {
return (T[]) JSONConverter.toArray(this, a.getClass().getComponentType());
}
@@ -600,7 +600,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
init((JSONTokener) source);
} else {
Iterator<?> iter;
if (source.getClass().isArray()) {// 数组
if (ArrayUtil.isArray(source)) {// 数组
iter = new ArrayIter<>(source);
} else if (source instanceof Iterator<?>) {// Iterator
iter = ((Iterator<?>) source);

View File

@@ -46,7 +46,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
/** JSON的KV持有Map */
private final Map<String, Object> rawHashMap;
/** 配置项 */
private JSONConfig config;
private final JSONConfig config;
// -------------------------------------------------------------------------------------------------------------------- Constructor start
/**
@@ -677,9 +677,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
} else if (source instanceof JSONTokener) {
// JSONTokener
init((JSONTokener) source);
} else if (source instanceof Number) {
// ignore Number
} else {
} else if(BeanUtil.isReadableBean(source.getClass())){
// 普通Bean
this.populateMap(source);
}

View File

@@ -11,7 +11,7 @@ public class CustomSerializeTest {
@Test
public void serializeTest() {
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.put("customName", bean.name));
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.set("customName", bean.name));
CustomBean customBean = new CustomBean();
customBean.name = "testName";

View File

@@ -25,6 +25,24 @@ public class JSONUtilTest {
Console.log(jsonArray);
}
/**
* 数字解析为JSONArray报错
*/
@Test(expected = JSONException.class)
public void parseNumberTest(){
JSONArray json = JSONUtil.parseArray(123L);
Console.log(json);
}
/**
* 数字解析为JSONObject忽略
*/
@Test
public void parseNumberTest2(){
JSONObject json = JSONUtil.parseObj(123L);
Assert.assertEquals(new JSONObject(), json);
}
@Test
public void toJsonStrTest() {
UserA a1 = new UserA();
@@ -67,9 +85,9 @@ public class JSONUtilTest {
public void toJsonStrTest3() {
// 验证某个字段为JSON字符串时转义是否规范
JSONObject object = new JSONObject(true);
object.put("name", "123123");
object.put("value", "\\");
object.put("value2", "</");
object.set("name", "123123");
object.set("value", "\\");
object.set("value2", "</");
HashMap<String, String> map = MapUtil.newHashMap();
map.put("user", object.toString());