This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
TypeReference
高铁 edited this page Jul 3, 2017
·
8 revisions
在fastjson中提供了一个用于处理泛型反序列化的类TypeReference。
import com.alibaba.fastjson.TypeReference;
List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});
如下写法有更好的性能
import com.alibaba.fastjson.TypeReference;
final static Type type = new TypeReference<List<VO>>() {}.getType();
List<VO> list = JSON.parseObject("...", type);
在这里例子中,通过TypeReference能够解决List中T的类型问题。
在1.2.9 & 1.1.49.android版本中,TypeReference支持泛型参数,方便一些框架实现通用的反序列化类。用法如下:
public class Response<T> {
public T data;
}
public static <T> Response<T> parseToMap(String json, Class<T> type) {
return JSON.parseObject(json,
new TypeReference<Response<T>>(type) {});
}
public static <K, V> Map<K, V> parseToMap(String json,
Class<K> keyType,
Class<V> valueType) {
return JSON.parseObject(json,
new TypeReference<Map<K, V>>(keyType, valueType) {
});
}
// 可以这样使用
String json = "{1:{name:\"ddd\"},2:{name:\"zzz\"}}";
Map<Integer, Model> map = parseToMap(json, Integer.class, Model.class);
assertEquals("ddd", map.get(1).name);
assertEquals("zzz", map.get(2).name);
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner