FastJson---TypeReference

概述

  Represents a generic type {@code T}.  代表泛型类型T

  Java doesn't yet provide a way to represent generic types, so this class does.  Java未提供一种方式表示 泛型类型

  Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.  强制client创建一个该类的子类,以便运行时可以检索到类型

  For example, to create a type literal for {@code List<String>}, you can create an empty anonymous inner class:  比如创建List<String>,可以创建一个空的匿名内部类

    TypeReference<List<String>> list = new TypeReference<List<String>>() {};

  This syntax cannot be used to create type literals that have wildcard parameters, such as {@code Class <?>} or {@code List <? extends CharSequence>}.

    ******不能创建 带有 通配符 的泛型类型

why

  使用 fastjson 反序列化一个 泛型类 时,执行时会发现反序列化出来的列表元素的是 JSONObject,而不是期望的 User;

    String userString = "[{\"userId\":\"111\",\"userName\":\"Tom\"},{\"userId\":\"222\",\"userName\":\"Jerry\"}]";

    List<User> userInfoList = JSON.parseObject(userString, List.class);

    

  为了处理这种问题,fastjson提供了 TypeReference,一个用于处理 泛型反序列化的类

    String userString = "[{\"userId\":\"111\",\"userName\":\"Tom\"},{\"userId\":\"222\",\"userName\":\"Jerry\"}]";

    List<User> userInfoList = JSON.parseObject(userString, new TypeReference<List<User>>() {});

    

How

匿名内部类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 基于接口
 new Runnable(){
     @Override
     public void run() {
 
     }
 };
 
 // 基于抽象类
 new AbstractList<String>() {
     @Override
     public int size() {
         return 0;
     }
 
     @Override
     public String get(int index) {
         return null;
     }
 };
 
 // 基于 非抽象类
 new ArrayList<String>(){
 
 }       

  

  new TypeReference<List<User>>() {}

    com.alibaba.fastjson.TypeReference#TypeReference() 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class TypeReference<T> {
    static ConcurrentMap<Type, Type> classTypeCache = new ConcurrentHashMap<Type, Type>(16, 0.75f, 1);
 
    protected final Type type;
 
    /**
     * Constructs a new type literal. Derives represented class from type
     * parameter.  构建一个新的类型,从 类型参数 派生;
     *
     * <p>Clients create an empty anonymous subclass. Doing so embeds the type
     * parameter in the anonymous class's type hierarchy so we can reconstitute it
     * at runtime despite erasure.  客户端创建一个空的匿名子类
     */
    protected TypeReference(){
        // 获取代表 当前class的父类 的type
        Type superClass = getClass().getGenericSuperclass();
        // 获取 该type的真实 类型参数
        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;
    }
 
}

  

 

 

  

posted on   anpeiyong  阅读(1174)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-10-07 JDK官方文档

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示