关于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);
// 反序列化
// 使用TypeReference
Generic generic = objectMapper.readValue(string, new TypeReference<Generic<List<Student>>>() {
});
System.out.println("使用TypeReference" + generic);
// 不使用TypeReference
Generic 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来指定或引用给某一对象.