mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复Pair反序列化报错问题
This commit is contained in:
@@ -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));
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user