mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix TypeReference not fit Type
This commit is contained in:
parent
a91686a7e1
commit
e42c0216db
@ -24,6 +24,7 @@
|
||||
* 【cache】 修复missCount规则(issue#465@Github)
|
||||
* 【core】 修复父目录拷贝到子目录导致的递归问题
|
||||
* 【crypto】 修复RSA中分段加密计算导致的异常(issue#481@Github)
|
||||
* 【json】 修复TypeReference传入Type类型参数导致的异常(issue#488@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -51,6 +51,7 @@ import cn.hutool.core.convert.impl.URIConverter;
|
||||
import cn.hutool.core.convert.impl.URLConverter;
|
||||
import cn.hutool.core.convert.impl.UUIDConverter;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
@ -199,6 +200,10 @@ public class ConverterRegistry implements Serializable{
|
||||
type = defaultValue.getClass();
|
||||
}
|
||||
|
||||
if(type instanceof TypeReference) {
|
||||
type = ((TypeReference<?>)type).getType();
|
||||
}
|
||||
|
||||
// 标准转换器
|
||||
final Converter<T> converter = getConverter(type, isCustomFirst);
|
||||
if (null != converter) {
|
||||
|
@ -7,6 +7,7 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
|
||||
/**
|
||||
* 类型转换工具单元测试
|
||||
@ -26,13 +27,15 @@ public class ConvertTest {
|
||||
public void toStrTest() {
|
||||
int a = 1;
|
||||
long[] b = { 1, 2, 3, 4, 5 };
|
||||
|
||||
Console.log(Convert.convert(String.class, b));
|
||||
|
||||
String aStr = Convert.toStr(a);
|
||||
Assert.assertEquals("1", aStr);
|
||||
String bStr = Convert.toStr(b);
|
||||
Assert.assertEquals("[1, 2, 3, 4, 5]", Convert.toStr(bStr));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toStrTest2() {
|
||||
String result = Convert.convert(String.class, "aaaa");
|
||||
|
@ -16,6 +16,7 @@ import java.util.ResourceBundle;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@ -342,6 +343,19 @@ public final class JSONUtil {
|
||||
public static <T> T toBean(JSONObject json, Class<T> beanClass) {
|
||||
return null == json ? null : json.toBean(beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param typeReference {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(String jsonString, TypeReference<T> typeReference, boolean ignoreError) {
|
||||
return toBean(jsonString, typeReference.getType(), ignoreError);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出
|
||||
@ -355,6 +369,20 @@ public final class JSONUtil {
|
||||
public static <T> T toBean(String jsonString, Type beanType, boolean ignoreError) {
|
||||
return toBean(parseObj(jsonString), beanType, ignoreError);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param json JSONObject
|
||||
* @param typeReference {@link TypeReference}类型参考子类,可以获取其泛型参数中的Type类型
|
||||
* @param ignoreError 是否忽略转换错误
|
||||
* @return 实体类对象
|
||||
* @since 4.6.2
|
||||
*/
|
||||
public static <T> T toBean(JSONObject json, TypeReference<T> typeReference, boolean ignoreError) {
|
||||
return toBean(json, typeReference.getType(), ignoreError);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
|
45
hutool-json/src/test/java/cn/hutool/json/Issue488Test.java
Normal file
45
hutool-json/src/test/java/cn/hutool/json/Issue488Test.java
Normal file
@ -0,0 +1,45 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import lombok.Data;
|
||||
|
||||
public class Issue488Test {
|
||||
|
||||
@Test
|
||||
public void toBeanTest() {
|
||||
String jsonStr = ResourceUtil.readUtf8Str("issue488.json");
|
||||
|
||||
ResultSuccess<List<EmailAddress>> result = JSONUtil.toBean(jsonStr,
|
||||
new TypeReference<ResultSuccess<List<EmailAddress>>>() {}, false);
|
||||
|
||||
Assert.assertEquals("https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)", result.getContext());
|
||||
|
||||
List<EmailAddress> adds = result.getValue();
|
||||
Assert.assertEquals("会议室101", adds.get(0).getName());
|
||||
Assert.assertEquals("MeetingRoom101@abc.com", adds.get(0).getAddress());
|
||||
Assert.assertEquals("会议室102", adds.get(1).getName());
|
||||
Assert.assertEquals("MeetingRoom102@abc.com", adds.get(1).getAddress());
|
||||
Assert.assertEquals("会议室103", adds.get(2).getName());
|
||||
Assert.assertEquals("MeetingRoom103@abc.com", adds.get(2).getAddress());
|
||||
Assert.assertEquals("会议室219", adds.get(3).getName());
|
||||
Assert.assertEquals("MeetingRoom219@abc.com", adds.get(3).getAddress());
|
||||
}
|
||||
|
||||
@Data
|
||||
public class ResultSuccess<T> {
|
||||
private String context;
|
||||
private T value;
|
||||
}
|
||||
|
||||
@Data
|
||||
public class EmailAddress {
|
||||
private String name;
|
||||
private String address;
|
||||
}
|
||||
}
|
16
hutool-json/src/test/resources/issue488.json
Normal file
16
hutool-json/src/test/resources/issue488.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)",
|
||||
"value": [{
|
||||
"name": "\u4f1a\u8bae\u5ba4101",
|
||||
"address": "MeetingRoom101@abc.com"
|
||||
}, {
|
||||
"name": "\u4f1a\u8bae\u5ba4102",
|
||||
"address": "MeetingRoom102@abc.com"
|
||||
}, {
|
||||
"name": "\u4f1a\u8bae\u5ba4103",
|
||||
"address": "MeetingRoom103@abc.com"
|
||||
}, {
|
||||
"name": "\u4f1a\u8bae\u5ba4219",
|
||||
"address": "MeetingRoom219@abc.com"
|
||||
}]
|
||||
}
|
Loading…
Reference in New Issue
Block a user