網站首頁 編程語言 正文
今天在轉換json串的時候,看到了一種寫法
MessageVO<String> messageVo = JSON.parseObject(msgContent, new TypeReference<MessageVO<String>>() {});
在最后邊加了一對 ’ {} ’ ,剛開始對這個很迷惑,但是看了TypeReference的源碼之后明白了
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;
}
protected TypeReference(Type... actualTypeArguments) {
Class<?> thisClass = this.getClass();
Type superClass = thisClass.getGenericSuperclass();
ParameterizedType argType = (ParameterizedType)((ParameterizedType)superClass).getActualTypeArguments()[0];
Type rawType = argType.getRawType();
Type[] argTypes = argType.getActualTypeArguments();
int actualIndex = 0;
for(int i = 0; i < argTypes.length; ++i) {
if (argTypes[i] instanceof TypeVariable && actualIndex < actualTypeArguments.length) {
argTypes[i] = actualTypeArguments[actualIndex++];
}
if (argTypes[i] instanceof GenericArrayType) {
argTypes[i] = TypeUtils.checkPrimitiveArray((GenericArrayType)argTypes[i]);
}
if (argTypes[i] instanceof ParameterizedType) {
argTypes[i] = this.handlerParameterizedType((ParameterizedType)argTypes[i], actualTypeArguments, actualIndex);
}
}
Type key = new ParameterizedTypeImpl(argTypes, thisClass, rawType);
Type cachedType = (Type)classTypeCache.get(key);
if (cachedType == null) {
classTypeCache.putIfAbsent(key, key);
cachedType = (Type)classTypeCache.get(key);
}
this.type = cachedType;
}
我們可以看到他的構造方法使用protect方法修飾,也就是只能在當錢包中才能訪問,別的包就不可以了,但是我們可以使用創(chuàng)建一個子類的方法去使用TypeReference的構造方法。
import com.alibaba.fastjson.TypeReference;
public class TypeTestChild<T> extends TypeReference<T> {
}
那么當我們寫代碼時,就可以使用子類的構造方法去訪問父類的構造方法;
Map<String, Object> objectMapTest = JSON.parseObject(responseStr, new TypeTestChild<Map<String, Object>>());
這也就是我們之前看到的
MessageVO<String> messageVo = JSON.parseObject(msgContent, new TypeReference<MessageVO<String>>() {});```
原文鏈接:https://blog.csdn.net/weixin_43790562/article/details/125984041
相關推薦
- 2022-08-20 C++詳細講解內存管理工具primitives_C 語言
- 2021-12-09 android藍牙簡單開發(fā)示例教程_Android
- 2022-03-22 C語言return,?exit,?abort的區(qū)別_C 語言
- 2022-08-26 C++超集C++/CLI模塊的基本語法_C 語言
- 2022-06-16 C語言學習筆記之字符串間的那些事_C 語言
- 2022-11-01 詳解C++中函數模板的定義與使用_C 語言
- 2022-05-04 Jupyter?notebook運行后打不開網頁的問題解決_python
- 2023-04-06 Android基準配置文件Baseline?Profile方案提升啟動速度_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支