網(wǎng)站首頁 編程語言 正文
今天在轉(zhuǎn)換json串的時(shí)候,看到了一種寫法
MessageVO<String> messageVo = JSON.parseObject(msgContent, new TypeReference<MessageVO<String>>() {});
在最后邊加了一對 ’ {} ’ ,剛開始對這個(gè)很迷惑,但是看了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;
}
我們可以看到他的構(gòu)造方法使用protect方法修飾,也就是只能在當(dāng)錢包中才能訪問,別的包就不可以了,但是我們可以使用創(chuàng)建一個(gè)子類的方法去使用TypeReference的構(gòu)造方法。
import com.alibaba.fastjson.TypeReference;
public class TypeTestChild<T> extends TypeReference<T> {
}
那么當(dāng)我們寫代碼時(shí),就可以使用子類的構(gòu)造方法去訪問父類的構(gòu)造方法;
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
相關(guān)推薦
- 2023-10-11 Mybatis-Plus條件構(gòu)造器的select
- 2022-03-15 C語言if選擇結(jié)構(gòu)語句詳解_C 語言
- 2022-02-11 安裝element UI (全局引入與按需引入)
- 2022-03-16 Oracle數(shù)據(jù)庫分析函數(shù)用法_oracle
- 2022-06-01 python中的mock接口開發(fā)示例詳解_python
- 2022-12-23 Kubernetes存儲(chǔ)系統(tǒng)數(shù)據(jù)持久化管理詳解_云其它
- 2022-11-04 ASP.NET?MVC解決上傳圖片臟數(shù)據(jù)的方法_實(shí)用技巧
- 2022-05-13 數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記——順序存儲(chǔ)結(jié)構(gòu)實(shí)現(xiàn)棧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支