修复序列化和反序列化Class问题

This commit is contained in:
Looly 2023-06-25 18:08:27 +08:00
parent 7ab95e5cb8
commit 4982f49a37
3 changed files with 39 additions and 1 deletions

View File

@ -9,6 +9,7 @@
### 🐞Bug修复
* 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题issue#3162@Github
* 【core 】 修复序列化和反序列化Class问题issue#I7FQ29@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.20(2023-06-16)

View File

@ -3,6 +3,7 @@ package cn.hutool.core.convert;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.impl.*;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.lang.TypeReference;
@ -319,6 +320,12 @@ public class ConverterRegistry implements Serializable {
return (T) arrayConverter.convert(value, defaultValue);
}
// issue#I7FQ29 Class
if("java.lang.Class".equals(rowType.getName())){
final ClassConverter converter = new ClassConverter();
return (T) converter.convert(value, (Class<?>) defaultValue);
}
// 表示非需要特殊转换的对象
return null;
}
@ -398,7 +405,6 @@ public class ConverterRegistry implements Serializable {
defaultConverterMap.put(AtomicLongArray.class, new AtomicLongArrayConverter());
// 其它类型
defaultConverterMap.put(Class.class, new ClassConverter());
defaultConverterMap.put(TimeZone.class, new TimeZoneConverter());
defaultConverterMap.put(Locale.class, new LocaleConverter());
defaultConverterMap.put(Charset.class, new CharsetConverter());

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package cn.hutool.json;
import cn.hutool.core.lang.TypeReference;
import org.junit.Assert;
import org.junit.Test;
import java.util.Map;
public class IssueI7FQ29Test {
@Test
public void toMapTest() {
final String jsonStr = "{\"trans_no\": \"java.lang.String\"}";
final Map<String, Class<?>> map = JSONUtil.toBean(jsonStr, new TypeReference<Map<String, Class<?>>>() {
}, false);
Assert.assertNotNull(map);
Assert.assertEquals(String.class, map.get("trans_no"));
}
}