修复JSONUtil.toBean()中将JSON数组字符串转Map对象返回错误问题(issue#3795@Github)

This commit is contained in:
Looly 2024-11-24 16:01:28 +08:00
parent d11693fb2d
commit 50807c84d6
3 changed files with 23 additions and 0 deletions

View File

@ -17,6 +17,7 @@
* 【extra 】 修复MailUtil发送html格式邮件无法正常展示图片问题(pr#1279@Gitee)
* 【core 】 【可能的向下兼容问题】修复双引号转义符转义错误问题,修改规则后,对非闭合双引号字段的策略变更,如"aa则被识别为aa(issue#IB5UQ8@Gitee)
* 【extra 】 修复Sftp中传入Session重连时逻辑错误问题(issue#IB69U8@Gitee)
* 【json 】 修复JSONUtil.toBean()中将JSON数组字符串转Map对象返回错误问题(issue#3795@Github)
-------------------------------------------------------------------------------------------------------------
# 5.8.33(2024-11-05)

View File

@ -74,6 +74,11 @@ public class MapConverter extends AbstractConverter<Map<?, ?>> {
}
convertMapToMap((Map) value, map);
} else if (BeanUtil.isBean(value.getClass())) {
if(value.getClass().getName().equals("cn.hutool.json.JSONArray")){
// issue#3795 增加JSONArray转Map错误检查
throw new UnsupportedOperationException(StrUtil.format("Unsupported {} to Map.", value.getClass().getName()));
}
map = BeanUtil.beanToMap(value);
// 二次转换转换键值类型
map = convertInternal(map);

View File

@ -0,0 +1,17 @@
package cn.hutool.json;
import cn.hutool.core.lang.TypeReference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Map;
public class Issue3795Test {
@Test
void toBeanTest() {
String fieldMapping = "[{\"lable\":\"id\",\"value\":\"id\"},{\"lable\":\"name\",\"value\":\"name\"},{\"lable\":\"age\",\"value\":\"age\"}]";
Assertions.assertThrows(UnsupportedOperationException.class, ()->{
JSONUtil.toBean(fieldMapping, new TypeReference<Map<String, String>>() {}, false);
});
}
}