mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
update version
This commit is contained in:
@@ -65,7 +65,7 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (object instanceof JSONArray) {
|
||||
if (object instanceof JSON) {
|
||||
return (JSONArray) object;
|
||||
}
|
||||
return new JSONArray(object, getConfig());
|
||||
@@ -84,7 +84,7 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (object instanceof JSONObject) {
|
||||
if (object instanceof JSON) {
|
||||
return (JSONObject) object;
|
||||
}
|
||||
return new JSONObject(object, getConfig());
|
||||
|
@@ -24,6 +24,7 @@ import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
@@ -644,11 +645,19 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
return;
|
||||
}
|
||||
|
||||
// 自定义序列化
|
||||
final JSONSerializer serializer = GlobalSerializeMapping.getSerializer(source.getClass());
|
||||
if (serializer instanceof JSONObjectSerializer) {
|
||||
// 自定义序列化
|
||||
serializer.serialize(this, source);
|
||||
} else if (source instanceof Map) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(ArrayUtil.isArray(source) || source instanceof Iterable || source instanceof Iterator){
|
||||
// 不支持集合类型转换为JSONObject
|
||||
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
||||
}
|
||||
|
||||
if (source instanceof Map) {
|
||||
// Map
|
||||
for (final Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
this.set(Convert.toStr(e.getKey()), e.getValue());
|
||||
@@ -668,7 +677,11 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
} else if (BeanUtil.isReadableBean(source.getClass())) {
|
||||
// 普通Bean
|
||||
this.populateMap(source);
|
||||
} else {
|
||||
// 不支持对象类型转换为JSONObject
|
||||
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -25,6 +25,12 @@ import java.util.Map;
|
||||
*/
|
||||
public class JSONArrayTest {
|
||||
|
||||
@Test(expected = JSONException.class)
|
||||
public void createJSONArrayTest(){
|
||||
// 集合类不支持转为JSONObject
|
||||
new JSONArray(new JSONObject(), JSONConfig.create());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addTest() {
|
||||
// 方法1
|
||||
|
@@ -520,4 +520,10 @@ public class JSONObjectTest {
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(next);
|
||||
Console.log(jsonObject);
|
||||
}
|
||||
|
||||
@Test(expected = JSONException.class)
|
||||
public void createJSONObjectTest(){
|
||||
// 集合类不支持转为JSONObject
|
||||
new JSONObject(new JSONArray(), JSONConfig.create());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user