JsonUtil.java 743 B

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