fastjson为何使用TypeReference?(上)

简介: fastjson为何使用TypeReference?

1 核心接口及类

fastJson 的泛型反序列化场景经常使用到 TypeReference,如下示例:

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add("1");
    list.add("2");
    JSONObject o = new JSONObject();
    o.put("k",list);
    List<String> types = o.getObject("k",List.class);
    System.out.println(JSON.toJSONString(types));
    List<String> types2 = o.getObject("k",new TypeReference<List<String>>(){});
    System.out.println(JSON.toJSONString(types2));
}

使用TypeReference可明确指定反序列化的类型,如下


  • TypeReference构造器

image.png


1.1 ParameterizedType接口


ParameterizedType是一个记录类型泛型的接口, 继承自Type, 一共三接口


Type[] getActualTypeArguments

核心接口,返回泛型类型数组, 该接口可获取父类实际泛型类型,返回的Type数组对象表示该类型的实际类型参数。


Type getRawType()

返回原始类型Type


Type getOwnerType()

返回 Type 对象,表示此类型是其成员之一的类型。

image.png



比如 Map 响应ParameterizedType三个接口的返回值如下:

  • [class java.lang.String, class java.lang.String]
  • interface java.util.Map
  • null


fastjson对其的实现类


image.png


一般使用如下

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

创建一个TypeReference的匿名类,在其构造器中拿到泛型对应Type(java.lang.reflect.ParameterizedType)

TypeReference的存在是因为java中子类可以获取到父类泛型的真实类型,为便于理解,看一段测试代码

public class TypeReferenceKest {
    public static void main(String[] args) {
        IntMap intMap = new IntMap();
        System.out.println(intMap.getClass().getSuperclass());
        Type type = intMap.getClass().getGenericSuperclass();
        if(type instanceof ParameterizedType){
            ParameterizedType p = (ParameterizedType) type;
            for (Type t : p.getActualTypeArguments()){
                System.out.println(t);
            }
        }
        System.out.println("=====newclass=====");
        HashMap<String,Integer> newIntMap = new HashMap<>();
        System.out.println(newIntMap.getClass().getSuperclass());
        Type newClassType = newIntMap.getClass().getGenericSuperclass();
        if(newClassType instanceof ParameterizedType){
            ParameterizedType p = (ParameterizedType) newClassType;
            for (Type t : p.getActualTypeArguments()){
                System.out.println(t);
            }
        }
        System.out.println("=====subclass=====");
        HashMap<String,Integer> subIntMap = new HashMap<String,Integer>(){};
        System.out.println(subIntMap.getClass().getSuperclass());
        Type subClassType = subIntMap.getClass().getGenericSuperclass();
        if(subClassType instanceof ParameterizedType){
            ParameterizedType p = (ParameterizedType) subClassType;
            for (Type t : p.getActualTypeArguments()){
                System.out.println(t);
            }
        }
    }
    public static class IntMap extends HashMap<String,Integer> {
    }
}
目录
相关文章
|
JSON fastjson Java
FastJson、JackJson 以及 Gson 的区别
FastJson、JackJson 以及 Gson 是 Java 生态圈中三种常用的 Json 解析器,它们均可将 Java 对象序列化为 Json 格式的字符串,也可将 Json 字符串反序列化为 Java 对象。下面我们讨论一下三者在序列化和反序列化操作中的一些区别。
918 0
|
10天前
|
JSON fastjson Java
使用FastJson
使用FastJson
16 1
|
1月前
|
JSON fastjson Java
Gson与FastJson详解
综上,Gson和FastJson都是用于Java对象和JSON数据互相转换的优秀库,选择哪个取决于性能、功能需求和个人偏好。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
40 2
|
存储 缓存 JSON
fastjson2为什么这么快
fastjson2 提升速度的核心技术
75700 6
fastjson2为什么这么快
|
JSON 安全 fastjson
gson与fastjson
gson与fastjson
97 0
|
JSON fastjson Java
FastJson使用技巧
FastJson使用技巧
|
JSON 前端开发 Java
Jackson,Fastjson详细教程
1.Jackson 导入Maven依赖:
280 0
Jackson,Fastjson详细教程
|
fastjson Java
fastjson的使用
fastjson的使用
119 0
|
XML JSON 缓存
关于 FastJson
因为公司提供的基础框架使用的是 FastJson 框架、而部门的架构师推荐使用 Jackson。所以特此了解下 FastJson 相关的东西。
659 0
|
SQL JSON 缓存
fastjson学习笔记
JSON相信大家对他也不陌生了,前后端交互中常常就以JSON来进行数据交换。而有的时候,我们也会将JSON直接保存在数据库中。
275 0
fastjson学习笔记