mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-29 14:10:25 +08:00
@@ -122,7 +122,7 @@ public class TypeReference<T> {
|
||||
|
||||
// 如果有多层泛型且该泛型已经注明实现的情况下,判断该泛型下一层是否还有泛型
|
||||
if(argTypes[i] instanceof ParameterizedType) {
|
||||
return handlerParameterizedType((ParameterizedType) argTypes[i], actualTypeArguments, actualIndex);
|
||||
argTypes[i] = handlerParameterizedType((ParameterizedType) argTypes[i], actualTypeArguments, actualIndex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.alibaba.json.bvt.issue_3300;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author yumin.pym
|
||||
*/
|
||||
public class Issue3448 extends TestCase {
|
||||
public static class SelfTypeReference<T> {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
List<Map<String, List<String>>> list = new ArrayList<>(4);
|
||||
list.add(Collections.singletonMap("key1", Collections.singletonList("item")));
|
||||
String text = JSON.toJSONString(list);
|
||||
System.out.println("text = " + text);
|
||||
|
||||
List<Map<String, List<String>>> result = parseObject(text,
|
||||
new SelfTypeReference<Map<String, List<String>>>() {});
|
||||
System.out.println("result = " + result);
|
||||
TestCase.assertTrue(result.get(0) instanceof Map);
|
||||
TestCase.assertTrue(result.get(0).get("key1").get(0) instanceof String);
|
||||
}
|
||||
|
||||
public <T> List<T> parseObject(String text, SelfTypeReference<T> selfTypeReference) {
|
||||
Type genericSuperclass = selfTypeReference.getClass().getGenericSuperclass();
|
||||
Type[] actualTypeArguments = ((ParameterizedType)genericSuperclass).getActualTypeArguments();
|
||||
return JSON.parseObject(text, new TypeReference<List<T>>(actualTypeArguments) {});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user