修复Pair反序列化报错问题

This commit is contained in:
Looly
2023-04-05 01:05:07 +08:00
parent 84e8456377
commit a97901f0e3
7 changed files with 283 additions and 82 deletions

View File

@@ -17,6 +17,7 @@ import cn.hutool.json.serialize.JSONDeserializer;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
/**
* JSON转换器
@@ -122,7 +123,11 @@ public class JSONConverter implements Converter<JSON> {
// issue#2212@Github
// 在JSONObject转Bean时读取JSONObject本身的配置文件
if(value instanceof JSONGetter
&& targetType instanceof Class && BeanUtil.hasSetter((Class<?>) targetType)){
&& targetType instanceof Class
// Map.Entry特殊处理
&& (false == Map.Entry.class.isAssignableFrom((Class<?>)targetType)
&& BeanUtil.hasSetter((Class<?>) targetType))){
final JSONConfig config = ((JSONGetter<?>) value).getConfig();
final Converter<T> converter = new BeanConverter<>(targetType,
InternalJSONUtil.toCopyOptions(config).setIgnoreError(ignoreError));

View File

@@ -0,0 +1,30 @@
package cn.hutool.json;
import cn.hutool.core.lang.Pair;
import org.junit.Assert;
import org.junit.Test;
import java.util.AbstractMap;
import java.util.Map;
public class IssueI6SZYBTest {
@Test
public void pairTest() {
Pair<Integer,Integer> pair = Pair.of(1, 2);
String jsonStr = JSONUtil.toJsonStr(pair);
Assert.assertEquals("{\"key\":1,\"value\":2}", jsonStr);
final Pair bean = JSONUtil.toBean(jsonStr, Pair.class);
Assert.assertEquals(pair, bean);
}
@Test
public void entryTest() {
Map.Entry<String,Integer> pair = new AbstractMap.SimpleEntry<>("1", 2);
String jsonStr = JSONUtil.toJsonStr(pair);
Assert.assertEquals("{\"1\":2}", jsonStr);
final Map.Entry bean = JSONUtil.toBean(jsonStr, AbstractMap.SimpleEntry.class);
Assert.assertEquals(pair, bean);
}
}