This commit is contained in:
Looly 2022-06-07 10:41:00 +08:00
parent 886a865aac
commit f985887406
16 changed files with 44 additions and 67 deletions

View File

@ -243,7 +243,7 @@ public class ConverterRegistry implements Serializable {
}
// 无法转换
throw new ConvertException("Can not Converter from [{}] to [{}]", value.getClass().getName(), type.getTypeName());
throw new ConvertException("Can not convert from {}: [{}] to [{}]", value.getClass().getName(), value, type.getTypeName());
}
/**

View File

@ -201,7 +201,7 @@ public final class InternalJSONUtil {
static Map<String, Object> createRawMap(final int capacity, JSONConfig config) {
final Map<String, Object> rawHashMap;
if (null == config) {
config = JSONConfig.create();
config = JSONConfig.of();
}
final Comparator<String> keyComparator = config.getKeyComparator();
if (config.isIgnoreCase()) {

View File

@ -2,6 +2,8 @@ package cn.hutool.json;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.impl.ArrayConverter;
import cn.hutool.core.lang.func.Filter;
import cn.hutool.core.lang.mutable.Mutable;
import cn.hutool.core.lang.mutable.MutableObj;
@ -66,7 +68,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @since 3.2.2
*/
public JSONArray(final int initialCapacity) {
this(initialCapacity, JSONConfig.create());
this(initialCapacity, JSONConfig.of());
}
/**
@ -90,7 +92,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
*/
public JSONArray(final int initialCapacity, final JSONConfig config) {
this.rawList = new ArrayList<>(initialCapacity);
this.config = ObjUtil.defaultIfNull(config, JSONConfig::create);
this.config = ObjUtil.defaultIfNull(config, JSONConfig::of);
}
/**
@ -107,7 +109,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @throws JSONException 非数组或集合
*/
public JSONArray(final Object object) throws JSONException {
this(object, JSONConfig.create());
this(object, JSONConfig.of());
}
/**
@ -332,7 +334,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
@Override
@SuppressWarnings({"unchecked"})
public <T> T[] toArray(final T[] a) {
return (T[]) JSONConverter.toArray(this, a.getClass().getComponentType());
return (T[]) ArrayConverter.INSTANCE.convert(a.getClass().getComponentType(), this);
}
@Override
@ -481,7 +483,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @return 实体类对象
*/
public Object toArray(final Class<?> arrayClass) {
return JSONConverter.toArray(this, arrayClass);
return ArrayConverter.INSTANCE.convert(arrayClass, this);
}
/**
@ -493,7 +495,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
* @since 3.0.8
*/
public <T> List<T> toList(final Class<T> elementType) {
return JSONConverter.toList(this, elementType);
return Convert.toList(elementType, this);
}
/**

View File

@ -49,7 +49,7 @@ public class JSONConfig implements Serializable {
*
* @return JSONConfig
*/
public static JSONConfig create() {
public static JSONConfig of() {
return new JSONConfig();
}

View File

@ -6,7 +6,6 @@ import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.convert.ConverterRegistry;
import cn.hutool.core.convert.impl.ArrayConverter;
import cn.hutool.core.convert.impl.BeanConverter;
import cn.hutool.core.reflect.ConstructorUtil;
import cn.hutool.core.reflect.TypeUtil;
@ -16,7 +15,6 @@ import cn.hutool.json.serialize.GlobalSerializeMapping;
import cn.hutool.json.serialize.JSONDeserializer;
import java.lang.reflect.Type;
import java.util.List;
/**
* JSON转换器
@ -41,29 +39,6 @@ public class JSONConverter implements Converter {
return JSONUtil.parse(value);
}
/**
* JSONArray转数组
*
* @param jsonArray JSONArray
* @param arrayClass 数组元素类型
* @return 数组对象
*/
protected static Object toArray(final JSONArray jsonArray, final Class<?> arrayClass) {
return ArrayConverter.INSTANCE.convert(arrayClass, jsonArray, null);
}
/**
* 将JSONArray转换为指定类型的对量列表
*
* @param <T> 元素类型
* @param jsonArray JSONArray
* @param elementType 对象元素类型
* @return 对象列表
*/
protected static <T> List<T> toList(final JSONArray jsonArray, final Class<T> elementType) {
return Convert.toList(elementType, jsonArray);
}
/**
* JSON递归转换<br>
* 首先尝试JDK类型转换如果失败尝试JSON转Bean<br>

View File

@ -46,7 +46,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* 构造初始容量为 {@link #DEFAULT_CAPACITY}KEY有序
*/
public JSONObject() {
this(JSONConfig.create());
this(JSONConfig.of());
}
/**
@ -67,8 +67,8 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @since 4.1.19
*/
public JSONObject(final int capacity, final JSONConfig config) {
super(InternalJSONUtil.createRawMap(capacity, ObjUtil.defaultIfNull(config, JSONConfig.create())));
this.config = ObjUtil.defaultIfNull(config, JSONConfig.create());
super(InternalJSONUtil.createRawMap(capacity, ObjUtil.defaultIfNull(config, JSONConfig.of())));
this.config = ObjUtil.defaultIfNull(config, JSONConfig.of());
}
/**
@ -84,7 +84,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @param source JavaBean或者Map对象或者String
*/
public JSONObject(final Object source) {
this(source, JSONConfig.create().setIgnoreNullValue(InternalJSONUtil.defaultIgnoreNullValue(source)));
this(source, JSONConfig.of().setIgnoreNullValue(InternalJSONUtil.defaultIgnoreNullValue(source)));
}
/**

View File

@ -113,7 +113,7 @@ public class JSONUtil {
* @since 3.0.9
*/
public static JSONObject parseObj(final Object obj, final boolean ignoreNullValue) {
return new JSONObject(obj, JSONConfig.create().setIgnoreNullValue(ignoreNullValue));
return new JSONObject(obj, JSONConfig.of().setIgnoreNullValue(ignoreNullValue));
}
/**
@ -148,7 +148,7 @@ public class JSONUtil {
* @since 3.2.3
*/
public static JSONArray parseArray(final Object arrayOrCollection, final boolean ignoreNullValue) {
return new JSONArray(arrayOrCollection, JSONConfig.create().setIgnoreNullValue(ignoreNullValue));
return new JSONArray(arrayOrCollection, JSONConfig.of().setIgnoreNullValue(ignoreNullValue));
}
/**
@ -438,7 +438,7 @@ public class JSONUtil {
* @since 4.3.2
*/
public static <T> T toBean(final String jsonString, final Type beanType, final boolean ignoreError) {
return parse(jsonString, JSONConfig.create().setIgnoreError(ignoreError)).toBean(beanType);
return parse(jsonString, JSONConfig.of().setIgnoreError(ignoreError)).toBean(beanType);
}
/**

View File

@ -21,7 +21,7 @@ public class Claims implements Serializable {
private static final long serialVersionUID = 1L;
// 时间使用秒级时间戳表示
private final JSONConfig CONFIG = JSONConfig.create().setDateFormat("#sss");
private final JSONConfig CONFIG = JSONConfig.of().setDateFormat("#sss");
private JSONObject claimJSON;

View File

@ -19,7 +19,7 @@ public class Issue1075Test {
@Test
public void testToBeanIgnoreCase() {
// 在忽略大小写的情况下f2fac都匹配
final ObjA o2 = JSONUtil.parseObj(jsonStr, JSONConfig.create().setIgnoreCase(true)).toBean(ObjA.class);
final ObjA o2 = JSONUtil.parseObj(jsonStr, JSONConfig.of().setIgnoreCase(true)).toBean(ObjA.class);
Assert.assertEquals("fac", o2.getFAC());
Assert.assertEquals("f2", o2.getF2());

View File

@ -23,12 +23,12 @@ public class Issue2223Test {
map1.put("m1", m1);
Assert.assertEquals("{\"m1\":{\"2022/0\":0,\"2022/1\":1,\"2022/2\":2,\"2022/3\":3,\"2022/4\":4}}",
JSONUtil.toJsonStr(map1, JSONConfig.create()));
JSONUtil.toJsonStr(map1, JSONConfig.of()));
final BeanDemo beanDemo = new BeanDemo();
beanDemo.setMap1(map1);
Assert.assertEquals("{\"map1\":{\"m1\":{\"2022/0\":0,\"2022/1\":1,\"2022/2\":2,\"2022/3\":3,\"2022/4\":4}}}",
JSONUtil.toJsonStr(beanDemo, JSONConfig.create()));
JSONUtil.toJsonStr(beanDemo, JSONConfig.of()));
}
@Data

View File

@ -12,7 +12,7 @@ public class IssueI4RBZ4Test {
public void sortTest(){
final String jsonStr = "{\"id\":\"123\",\"array\":[1,2,3],\"outNum\":356,\"body\":{\"ava1\":\"20220108\",\"use\":1,\"ava2\":\"20230108\"},\"name\":\"John\"}";
final JSONObject jsonObject = JSONUtil.parseObj(jsonStr, JSONConfig.create().setNatureKeyComparator());
final JSONObject jsonObject = JSONUtil.parseObj(jsonStr, JSONConfig.of().setNatureKeyComparator());
Assert.assertEquals("{\"array\":[1,2,3],\"body\":{\"ava1\":\"20220108\",\"ava2\":\"20230108\",\"use\":1},\"id\":\"123\",\"name\":\"John\",\"outNum\":356}", jsonObject.toString());
}
}

View File

@ -10,7 +10,7 @@ public class IssueI50EGGTest {
@Test
public void toBeanTest(){
final String data = "{\"return_code\": 1, \"return_msg\": \"成功\", \"return_data\" : null}";
final ApiResult<?> apiResult = JSONUtil.toBean(data, JSONConfig.create().setIgnoreCase(true), ApiResult.class);
final ApiResult<?> apiResult = JSONUtil.toBean(data, JSONConfig.of().setIgnoreCase(true), ApiResult.class);
Assert.assertEquals(1, apiResult.getReturn_code());
}

View File

@ -31,11 +31,11 @@ public class JSONArrayTest {
// JSONObject实现了Iterable接口可以转换为JSONArray
final JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray(jsonObject, JSONConfig.create());
JSONArray jsonArray = new JSONArray(jsonObject, JSONConfig.of());
Assert.assertEquals(new JSONArray(), jsonArray);
jsonObject.set("key1", "value1");
jsonArray = new JSONArray(jsonObject, JSONConfig.create());
jsonArray = new JSONArray(jsonObject, JSONConfig.of());
Assert.assertEquals(1, jsonArray.size());
Assert.assertEquals("[{\"key1\":\"value1\"}]", jsonArray.toString());
}
@ -44,7 +44,7 @@ public class JSONArrayTest {
public void addNullTest(){
final List<String> aaa = ListUtil.view("aaa", null);
final String jsonStr = JSONUtil.toJsonStr(JSONUtil.parse(aaa,
JSONConfig.create().setIgnoreNullValue(false)));
JSONConfig.of().setIgnoreNullValue(false)));
Assert.assertEquals("[\"aaa\",null]", jsonStr);
}
@ -135,7 +135,7 @@ public class JSONArrayTest {
public void toDictListTest() {
final String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
final JSONArray array = JSONUtil.parseArray(jsonArr, JSONConfig.create().setIgnoreError(false));
final JSONArray array = JSONUtil.parseArray(jsonArr, JSONConfig.of().setIgnoreError(false));
final List<Dict> list = JSONUtil.toList(array, Dict.class);
@ -273,7 +273,7 @@ public class JSONArrayTest {
@Test
public void putNullTest(){
final JSONArray array = JSONUtil.createArray(JSONConfig.create().setIgnoreNullValue(false));
final JSONArray array = JSONUtil.createArray(JSONConfig.of().setIgnoreNullValue(false));
array.set(null);
Assert.assertEquals("[null]", array.toString());

View File

@ -199,7 +199,7 @@ public class JSONObjectTest {
@Test
public void toBeanTest() {
final JSONObject subJson = JSONUtil.createObj().set("value1", "strValue1").set("value2", "234");
final JSONObject json = JSONUtil.createObj().set("strValue", "strTest").set("intValue", 123)
final JSONObject json = JSONUtil.createObj(JSONConfig.of().setIgnoreError(true)).set("strValue", "strTest").set("intValue", 123)
// 测试空字符串转对象
.set("doubleValue", "")
.set("beanValue", subJson)
@ -453,7 +453,7 @@ public class JSONObjectTest {
@Test
public void setDateFormatTest() {
final JSONConfig jsonConfig = JSONConfig.create();
final JSONConfig jsonConfig = JSONConfig.of();
jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
final JSONObject json = new JSONObject(jsonConfig);
@ -465,7 +465,7 @@ public class JSONObjectTest {
@Test
public void setDateFormatTest2() {
final JSONConfig jsonConfig = JSONConfig.create();
final JSONConfig jsonConfig = JSONConfig.of();
jsonConfig.setDateFormat("yyyy#MM#dd");
final Date date = DateUtil.parse("2020-06-05 11:16:11");
@ -485,7 +485,7 @@ public class JSONObjectTest {
@Test
public void setCustomDateFormatTest() {
final JSONConfig jsonConfig = JSONConfig.create();
final JSONConfig jsonConfig = JSONConfig.of();
jsonConfig.setDateFormat("#sss");
final Date date = DateUtil.parse("2020-06-05 11:16:11");
@ -599,7 +599,7 @@ public class JSONObjectTest {
@Test(expected = JSONException.class)
public void createJSONObjectTest() {
// 集合类不支持转为JSONObject
new JSONObject(new JSONArray(), JSONConfig.create());
new JSONObject(new JSONArray(), JSONConfig.of());
}
@Test
@ -646,7 +646,7 @@ public class JSONObjectTest {
@Test
public void filterIncludeTest() {
final JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
.set("a", "value1")
.set("b", "value2")
.set("c", "value3")
@ -658,7 +658,7 @@ public class JSONObjectTest {
@Test
public void filterExcludeTest() {
final JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
.set("a", "value1")
.set("b", "value2")
.set("c", "value3")
@ -670,7 +670,7 @@ public class JSONObjectTest {
@Test
public void editTest() {
final JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
.set("a", "value1")
.set("b", "value2")
.set("c", "value3")
@ -690,7 +690,7 @@ public class JSONObjectTest {
@Test
public void toUnderLineCaseTest() {
final JSONObject json1 = JSONUtil.createObj(JSONConfig.create())
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of())
.set("aKey", "value1")
.set("bJob", "value2")
.set("cGood", "value3")
@ -705,7 +705,7 @@ public class JSONObjectTest {
@Test
public void nullToEmptyTest() {
final JSONObject json1 = JSONUtil.createObj(JSONConfig.create().setIgnoreNullValue(false))
final JSONObject json1 = JSONUtil.createObj(JSONConfig.of().setIgnoreNullValue(false))
.set("a", null)
.set("b", "value2");

View File

@ -191,7 +191,7 @@ public class JSONUtilTest {
Assert.assertEquals("{\"test2\":12}", jsonObjectDefault.toString());
// 不去除多余的0
final JSONObject jsonObject = JSONUtil.createObj(JSONConfig.create().setStripTrailingZeros(false))
final JSONObject jsonObject = JSONUtil.createObj(JSONConfig.of().setStripTrailingZeros(false))
.set("test2", 12.00D);
Assert.assertEquals("{\"test2\":12.0}", jsonObject.toString());

View File

@ -20,7 +20,7 @@ public class TransientTest {
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = new JSONObject(detailBill,
JSONConfig.create().setTransientSupport(false));
JSONConfig.of().setTransientSupport(false));
Assert.assertEquals("{\"id\":\"3243\",\"bizNo\":\"bizNo\"}", jsonObject.toString());
}
@ -32,7 +32,7 @@ public class TransientTest {
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = new JSONObject(detailBill,
JSONConfig.create().setTransientSupport(true));
JSONConfig.of().setTransientSupport(true));
Assert.assertEquals("{\"bizNo\":\"bizNo\"}", jsonObject.toString());
}
@ -43,7 +43,7 @@ public class TransientTest {
detailBill.setBizNo("bizNo");
final JSONObject jsonObject = new JSONObject(detailBill,
JSONConfig.create().setTransientSupport(false));
JSONConfig.of().setTransientSupport(false));
final Bill bill = jsonObject.toBean(Bill.class);
Assert.assertEquals("3243", bill.getId());
@ -57,7 +57,7 @@ public class TransientTest {
detailBill.setBizNo("bizNo");
final JSONObject jsonObject = new JSONObject(detailBill,
JSONConfig.create().setTransientSupport(true));
JSONConfig.of().setTransientSupport(true));
final Bill bill = jsonObject.toBean(Bill.class);
Assert.assertNull(bill.getId());