当前位置: 首页 > news >正文

关于TypeReference的使用

关于TypeReference的使用

在项目中,有遇到TypeReference的使用,其主要在字符串转对象过程中,对于序列化和反序列化中也有效果,将字符串转换成自定义对象.

1 说明

以常见为例,在com.alibaba.fastjson包下面的TypeReference类,是指Type的Reference,表示某类型的一个指向或者引用.

    protected TypeReference() {// 当前类父类的类型Type superClass = this.getClass().getGenericSuperclass();Type type = ((ParameterizedType)superClass).getActualTypeArguments()[0];Type cachedType = (Type)classTypeCache.get(type);if (cachedType == null) {classTypeCache.putIfAbsent(type, type);cachedType = (Type)classTypeCache.get(type);}this.type = cachedType;}// 返回类型public Type getType()

2 使用

1 常见字符串转对象

实体类

@Data
public class Student {@ExcelProperty("姓名")private String name;@ExcelProperty("描述")private  String des;
}

通用类

@Data
public class Generic<T> {private String id;private T t;
}

测试类

    public static void main(String[] args)  {// 创建对象设置值Generic<List<Student>> objectGeneric = new Generic<>();objectGeneric.setId("0001");List<Student> studentList = new ArrayList<>();studentList.add(new Student("李白","唐"));studentList.add(new Student("王维","唐"));objectGeneric.setT(studentList);// 字符串String string1 = JSON.toJSONString(objectGeneric);System.out.println(string1);// 不指定类型JSONObject jsonObject = JSON.parseObject(string1);System.out.println(jsonObject);// 指定类型Generic<List<Student>> listGeneric = JSON.parseObject(string1, new com.alibaba.fastjson.TypeReference<Generic<List<Student>>>() {});System.out.println(listGeneric);/*
运行结果:
{"id":"0001","t":[{"creatTime":1701172903084,"des":"唐","name":"李白"},{"creatTime":1701172903084,"des":"唐","name":"王维"}]}{"t":[{"des":"唐","creatTime":1701172903084,"name":"李白"},{"des":"唐","creatTime":1701172903084,"name":"王维"}],"id":"0001"}Generic(id=0001, t=[Student{name='李白', des='唐', creatTime=Tue Nov 28 20:01:43 CST 2023}, Student{name='王维', des='唐', creatTime=Tue Nov 28 20:01:43 CST 2023}])
*/
}

2 常见序列化和反序列化

此处TypeReference类是com.fasterxml.jackson.core.type包下面的.

模拟参数同上.

测试类

    public static void main(String[] args) throws JsonProcessingException {// 创建对象设置值Generic<List<Student>> objectGeneric = new Generic<>();objectGeneric.setId("0001");List<Student> studentList = new ArrayList<>();studentList.add(new Student("李白","唐"));studentList.add(new Student("王维","唐"));objectGeneric.setT(studentList);ObjectMapper objectMapper = new ObjectMapper();// 序列化String string = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectGeneric);System.out.println(string);// 反序列化// 使用TypeReferenceGeneric generic = objectMapper.readValue(string, new TypeReference<Generic<List<Student>>>() {});System.out.println("使用TypeReference" + generic);// 不使用TypeReferenceGeneric generic2 = objectMapper.readValue(string, Generic.class);System.out.println("不使用TypeReference" + generic2);/*
运行结果:{"id" : "0001","t" : [ {"name" : "李白","des" : "唐","creatTime" : 1701173940428}, {"name" : "王维","des" : "唐","creatTime" : 1701173940428} ]
}使用TypeReferenceGeneric(id=0001, t=[Student{name='李白', des='唐', creatTime=Tue Nov 28 20:19:00 CST 2023}, Student{name='王维', des='唐', creatTime=Tue Nov 28 20:19:00 CST 2023}])不使用TypeReferenceGeneric(id=0001, t=[{name=李白, des=唐, creatTime=1701173940428}, {name=王维, des=唐, creatTime=1701173940428}])
*/}

在JSON字符串转自定义对象过程中, 和对象序列化与反序列化中, 都可以使用TypeReference来指定或引用给某一对象.

http://www.lryc.cn/news/305084.html

相关文章:

  • 阿里大文娱前端一面
  • Clickhouse系列之连接工具连接、数据类型和数据库
  • 【深入理解设计模式】原型设计模式
  • Python算法题集_图论(课程表)
  • 视频评论挖掘软件|抖音视频下载工具
  • Linux学习方法-框架学习法——Linux驱动架构的演进
  • Spring Boot基础面试问题(一)
  • 电路设计(28)——交通灯控制器的multisim仿真
  • 【Docker】免费使用的腾讯云容器镜像服务
  • 如何让qml使用opengl es
  • 金航标电子位于广西柳州鹿寨县天线生产基地于大年正月初九开工了!!
  • FlinkCDC详解
  • 力扣代码学习日记六
  • 「Python系列」Python标准库
  • 虚拟列表【vue】等高虚拟列表/非等高虚拟列表
  • 【MySQL】如何理解索引(高频面试点)
  • NXP实战笔记(四):S32K3xx如何产生中心对称三相六路波形
  • 关于uniapp H5应用无法在触摸屏正常显示的处理办法
  • Stable Diffusion 3 发布,AI生图效果,再次到达全新里程碑!
  • 单例模式怎样实现单例(独例)?
  • MySQL——基础内容
  • node 之 初步认识
  • css复习
  • HTML5和CSS3提高
  • 感受2024生物发酵展示会-明章机械
  • 算法打卡day1|数组篇|Leetcode 704.二分查找、27.移除元素
  • 什么是高阶组件
  • python实现裂区试验方差分析
  • Vue v-for、v-if、v-show常见问题
  • GPT技术在学术研究中的革命性应用:开启论文创作新篇章