Java中TypeReference用法詳情說明
在使用fastJson時,對於泛型的反序列化很多場景下都會使用到TypeReference,例如:
void testTypeReference() { List<Integer> list = new ArrayList<>(); list.add(1); list.add(9); list.add(4); list.add(8); JSONObject jsonObj = new JSONObject(); jsonObj.put("a", list); System.out.println(jsonObj); List<String> list2 = jsonObj.getObject("a", new TypeReference<List<Integer>>(){}); System.out.println(list2); } //輸出 1{"a":[1,9,4,8]} 2[1, 9, 4, 8]
{"msg":"","code":"0","data":[{"adjEq":"","details":[],"imr":"","isoEq":"","mgnRatio":"","mmr":"","notionalUsd":"","ordFroz":"","totalEq":"0","uTime":"1658332171773"}]} //拿code的值 String result1 = JSON.toJSONString(result); System.out.println("result1=="+result1); Result result2 = JSON.parseObject(result1, new TypeReference<Result>() {}); System.out.println(result2.getCode()); //拿data的值 Balance是data中數據的實體類 Result<List<Balance>> listResult = JSON.parseObject(result1, new TypeReference<Result<List<Balance>>>() {}); System.out.println("result2=="+listResult.getData());
使用TypeReference可以明確的指定反序列化的類型,具體實現邏輯參考TypeReference的構造函數
protected TypeReference(){ Type superClass = getClass().getGenericSuperclass(); Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0]; Type cachedType = classTypeCache.get(type); if (cachedType == null) { classTypeCache.putIfAbsent(type, type); cachedType = classTypeCache.get(type); } this.type = cachedType; }
解說:
其中核心的方法是:getActualTypeArguments,它可以得到父類的反省類型
ParameterizedType是一個記錄類型泛型的接口, 繼承自Type,一共三方法:
Type[] getActualTypeArguments(); //返回泛型類型數組 Type getRawType(); //返回原始類型Type Type getOwnerType(); //返回 Type 對象,表示此類型是其成員之一的類型。
例如 Map<String,String> 對應的ParameterizedType三個方法分別取值如下:
[class java.lang.String, class java.lang.String] interface java.util.Map null
例證:
package JsonLearn; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; public class TypeReferencBaseLearn { public static class IntMap extends HashMap<String, Integer> {} void test1() { IntMap intMap = new IntMap(); System.out.println(“getSuperclass:” + intMap.getClass().getSuperclass()); System.out.println(“getGenericSuperclass:” + intMap.getClass().getGenericSuperclass()); Type type = intMap.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType)type; for (Type t : p.getActualTypeArguments()) { System.out.println(t); } } } void test2() { Map<String, Integer> intMap = new HashMap<>(); System.out.println(“\ngetSuperclass:” + intMap.getClass().getSuperclass()); System.out.println(“getGenericSuperclass:” + intMap.getClass().getGenericSuperclass()); Type type = intMap.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType)type; for (Type t : p.getActualTypeArguments()) { System.out.println(t); } } } void test3() { Map<String, Integer> intMap = new HashMap<String, Integer>(){}; System.out.println(“\ngetSuperclass:” + intMap.getClass().getSuperclass()); System.out.println(“getGenericSuperclass:” + intMap.getClass().getGenericSuperclass()); Type type = intMap.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType)type; for (Type t : p.getActualTypeArguments()) { System.out.println(t); } } } public static void main(String[] args) { TypeReferencBaseLearn obj = new TypeReferencBaseLearn(); obj.test1(); obj.test2(); obj.test3(); } }
輸出:
getSuperclass:class java.util.HashMap
getGenericSuperclass:java.util.HashMap<java.lang.String, java.lang.Integer>
class java.lang.String
class java.lang.IntegergetSuperclass:class java.util.AbstractMap
getGenericSuperclass:java.util.AbstractMap<K, V>
K
VgetSuperclass:class java.util.HashMap
getGenericSuperclass:java.util.HashMap<java.lang.String, java.lang.Integer>
class java.lang.String
class java.lang.Integer
到此這篇關於Java中TypeReference用法詳情說明的文章就介紹到這瞭,更多相關Java TypeReference用法內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!