当前位置:网站首页>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

原网站

版权声明
本文为[Help me to the Internet cafe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091123334600.html