修复JSONUtil序列化和反序列化预期的结果不一致问题

This commit is contained in:
Looly 2024-03-06 17:20:09 +08:00
parent fa67339509
commit 923cf3d265
5 changed files with 12 additions and 6 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.27(2024-03-05)
# 5.8.27(2024-03-06)
### 🐣新特性
* 【extra 】 FreemarkerEngine修改默认版本参数
@ -11,6 +11,7 @@
### 🐞Bug修复
* 【core 】 修复PathMover对目标已存在且只读文件报错错误问题issue#I95CLT@Gitee
* 【json 】 修复JSONUtil序列化和反序列化预期的结果不一致问题pr#3507@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.26(2024-02-10)

View File

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

View File

@ -796,6 +796,7 @@ public class JSONUtil {
return object.toString();
}
// pr#3507
// Class类型保存类名
if (object instanceof Class<?>) {
return ((Class<?>) object).getName();

View File

@ -1,6 +1,7 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
/**
@ -14,8 +15,9 @@ public class Issue3504Test {
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));
final JsonBean bean = JSONUtil.toBean(huToolJsonStr, JsonBean.class);
Assert.assertNotNull(bean);
Assert.assertEquals("test", bean.getName());
}
@Data

View File

@ -1,6 +1,7 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
/**
@ -13,8 +14,9 @@ public class Issue3506Test {
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));
final Languages bean = JSONUtil.toBean(hutoolJSONString, Languages.class);
Assert.assertNotNull(bean);
Assert.assertEquals(bean.getLanguageType(), Java.class);
}
@Data