当前位置:网站首页>Range method returns the object conversion method
Range method returns the object conversion method
2022-06-09 12:05:00 【Help me to the Internet cafe】
I found a problem in the previous article , What clearly exists is a collection of objects , Why pass range Method returns LinkeHashMap
So through debug Look at the source code , Understand the execution process , Find out why
This is the author through debug Draw the general flow :
Look for the 8 in ,mapObject Method , Here is the return LinkeHashMap Why , Here we focus on the analysis of mapObject Method :

protected Object mapObject(JsonParser p, DeserializationContext ctxt) throws IOException {
// Get field name , That is to say key
String key1 = p.getText();
// Before execution token yes FIELD_NAME, That is, the field name
// And then execute nextToken() after , Change into VALUE_STRING, That is to say String Field value of type
// But if the value is not String type , Then it will be executed again later nextToken, Turn into VALUE_NUMBER_INT, That is, the integer field value
p.nextToken();
Object value1 = this.deserialize(p, ctxt);
String key2 = p.nextFieldName();
if (key2 == null) {
LinkedHashMap<String, Object> result = new LinkedHashMap(2);
result.put(key1, value1);
return result;
} else {
// Explain it above
p.nextToken();
// obtain value value
Object value2 = this.deserialize(p, ctxt);
String key = p.nextFieldName();
LinkedHashMap result;
if (key == null) {
result = new LinkedHashMap(4);
result.put(key1, value1);
result.put(key2, value2);
return result;
} else {
result = new LinkedHashMap();
result.put(key1, value1);
result.put(key2, value2);
do {
p.nextToken();
result.put(key, this.deserialize(p, ctxt));
} while((key = p.nextFieldName()) != null);
return result;
}
}
}
The first is this p That is to say JsonParser object , He is through JsonFactory Of createParser Method to create , Its function is to analyze Json character string , So before it's implemented mapObject I've already put value Parse and store JsonParser It's in .
JsonParser Explanatory reference :JSON How strings are parsed ?JsonParser Get to know _Java Technology blog -CSDN Blog _json parser
Then the execution process can refer to the notes
That's why I returned LinkeHashMap Why , So what's the solution
I'm gonna use it here ObjectMapper, stay range It is also seen in the flow chart of the method that it is actually used
adopt ObjectMapper Of convertValue Methods will LinkeHashMap Turn to target object :

@Test
public void testRange() {
String key = "right_push_all_01";
List<LinkedHashMap<String, Object>> linkedHashMapList = redisService.lRange(key, 0, -1);
ObjectMapper objectMapper = new ObjectMapper();
List<ThisIsDTO> thisIsDTOList = objectMapper.convertValue(linkedHashMapList, new TypeReference<List<ThisIsDTO>>() {
});
for (ThisIsDTO thisIsDTO : thisIsDTOList) {
System.out.println(thisIsDTO.getAge());
}
}
Be careful : Because it's a collection , So be sure to new TypeReference<List<ThisIsDTO>> To define the type of conversion
If it's just a single object , Can directly objectMapper.convertValue(linkedHashMap, ThisIsDTO.class)
Run the test :

It can be found that the conversion succeeded
边栏推荐
- 10 | 总结:中台落地工具资源汇总
- Preparation guide for 2022 soft test information security engineer examination
- 接力AlphaFold!星药科技重磅发布TBind-开启分子蛋白复合物结构预测新纪元
- Error in Library (patroon): there is no program package named 'patroon'
- 6. exchange the nodes in the linked list in pairs
- 企评家用杜邦分析法剖析:华东建筑集团股份有限公司企业财务状况
- dotnet core 也能协调分布式事务啦!
- 从2022年的这几篇论文看推荐系统序列建模的趋势
- VMware vSphere 6.5 configuration family
- PMP project management knowledge system
猜你喜欢

03 | definition of China Taiwan: what are we talking about when we talk about China Taiwan?

Error in library(patRoon) : 不存在叫‘patRoon‘这个名字的程辑包

08 | middle stage landing step 3: middle stage planning and design

1. < tag backtracking, combination and pruning > lt.77 Combination + pruning dyh

4.<tag-回溯和组合及其剪枝>lt.39.组合总和 + lt.40. 组合总和 II dbc

使用U盘一比一拷贝核心板系统镜像的方法

5.括号生成

5. < tag backtracking and cutting problems > lt.93 Restore IP address

Tag backtracking - brush questions to prepare knowledge -1 Backtracking template, + lt.46 Full Permutation

请你说说乐观锁和悲观锁,以及适用场景
随机推荐
xxl-job 使用初体验
go-zero 微服务实战系列(二、服务拆分)
08 | 中台落地第三步:中台的规划与设计(Design)
UDP可靠性实践
2. < tag backtracking, combination and pruning > lt.216 Total number of combinations|||
Computer quick index query software listary
01 | context: why is Zhongtai so popular?
HEVC之HM学习01
还有新产品?丰田GR系列或将扩充产品线
3.<tag-回溯和组合及其剪枝>lt.17. 电话号码的字母组合
6.两两交换链表中的节点
Preparation guide for 2022 soft test information security engineer examination
H3C certified cloud computing Engineer
匿名內部類與局部變量
13.<tag-二叉树和BST基础>lt.450. 删除二叉搜索树中的节点 dbc
spark 寫入doris太慢方案解决
[patch analysis] cve-2016-8610: patch analysis of "SSL death alert" vulnerability leading to denial of service
Security evaluation of commercial password application
Simple use of GDB
4.<tag-回溯和组合及其剪枝>lt.39.组合总和 + lt.40. 组合总和 II dbc