Merge branch 'v5-dev' of github.com:dromara/hutool into v5-dev

This commit is contained in:
Looly 2024-03-06 17:10:30 +08:00
commit fa67339509
4 changed files with 63 additions and 1 deletions

View File

@ -153,7 +153,8 @@ public class ConverterRegistry implements Serializable {
*/
@SuppressWarnings("unchecked")
public <T> Converter<T> getDefaultConverter(Type type) {
return (null == defaultConverterMap) ? null : (Converter<T>) defaultConverterMap.get(TypeUtil.getClass(type));
Class<?> key = TypeUtil.getClass(type);
return (null == defaultConverterMap || null == key) ? null : (Converter<T>) defaultConverterMap.get(key);
}
/**

View File

@ -796,6 +796,11 @@ public class JSONUtil {
return object.toString();
}
// Class类型保存类名
if (object instanceof Class<?>) {
return ((Class<?>) object).getName();
}
// Java内部类不做转换
if (ClassUtil.isJdkClass(object.getClass())) {
return object.toString();

View File

@ -0,0 +1,26 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.Test;
/**
* https://github.com/dromara/hutool/issues/3504
*/
public class Issue3504Test {
@Test
public void test3504() {
JsonBean jsonBean = new JsonBean();
jsonBean.setName("test");
jsonBean.setClasses(new Class[]{String.class});
String huToolJsonStr = JSONUtil.toJsonStr(jsonBean);
System.out.println("hutool json str-------" + huToolJsonStr);
System.out.println(JSONUtil.toBean(huToolJsonStr, JsonBean.class));
}
@Data
public static class JsonBean {
private String name;
private Class<?>[] classes;
}
}

View File

@ -0,0 +1,30 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.Test;
/**
* https://github.com/dromara/hutool/issues/3506
*/
public class Issue3506Test {
@Test
public void test3506() {
Languages languages = new Languages();
languages.setLanguageType(Java.class);
String hutoolJSONString = JSONUtil.toJsonStr(languages);
System.out.println(hutoolJSONString);
System.out.println(JSONUtil.toBean(hutoolJSONString, Languages.class));
}
@Data
public static class Languages {
private Class<? extends Language> languageType;
}
public interface Language {
}
public static class Java implements Language {
}
}