648540858
2023-09-11 88350873ee7632924cb135a450fef9f4d05c5306
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.genersoft.iot.vmp.utils;
 
import org.springframework.data.redis.core.RedisTemplate;
 
import java.util.Objects;
 
/**
 * JsonUtil
 *
 * @author KunLong-Luo
 * @version 1.0.0
 * @since 2023/2/2 15:24
 */
public final class JsonUtil {
 
    private JsonUtil() {
    }
 
    /**
     * safe json type conversion
     *
     * @param key   redis key
     * @param clazz cast type
     * @param <T>
     * @return result type
     */
    public static <T> T redisJsonToObject(RedisTemplate<Object, Object> redisTemplate, String key, Class<T> clazz) {
        Object jsonObject = redisTemplate.opsForValue().get(key);
        if (Objects.isNull(jsonObject)) {
            return null;
        }
        return clazz.cast(jsonObject);
    }
}