Blame view

src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java 1.84 KB
ec8f181f   YunaiV   完成操作日志的记录
1
2
  package cn.iocoder.dashboard.util.json;
  
a7d0f308   YunaiV   迁移 Redis 监控接口
3
4
  import cn.hutool.core.util.ArrayUtil;
  import cn.hutool.core.util.StrUtil;
eadc4f74   YunaiV   替换带代码里所有 fastjson...
5
  import com.fasterxml.jackson.core.JsonProcessingException;
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
6
  import com.fasterxml.jackson.core.type.TypeReference;
eadc4f74   YunaiV   替换带代码里所有 fastjson...
7
8
9
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  import java.io.IOException;
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
10
  import java.util.Set;
c10ab175   YunaiV   优化 Redis pub/sub ...
11
  
ec8f181f   YunaiV   完成操作日志的记录
12
13
14
15
16
  /**
   * JSON 工具类
   *
   * @author 芋道源码
   */
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
17
  public class JsonUtils {
ec8f181f   YunaiV   完成操作日志的记录
18
  
eadc4f74   YunaiV   替换带代码里所有 fastjson...
19
20
      private static ObjectMapper objectMapper = new ObjectMapper();
  
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
21
22
23
24
25
26
27
28
29
      /**
       * 初始化 objectMapper 属性
       *
       * 通过这样的方式,使用 Spring 创建的 ObjectMapper Bean
       *
       * @param objectMapper ObjectMapper 对象
       */
      public static void init(ObjectMapper objectMapper) {
          JsonUtils.objectMapper = objectMapper;
eadc4f74   YunaiV   替换带代码里所有 fastjson...
30
31
      }
  
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
32
      public static String toJsonString(Object object) {
eadc4f74   YunaiV   替换带代码里所有 fastjson...
33
34
35
36
37
          try {
              return objectMapper.writeValueAsString(object);
          } catch (JsonProcessingException e) {
              throw new RuntimeException(e);
          }
c10ab175   YunaiV   优化 Redis pub/sub ...
38
39
40
      }
  
      public static <T> T parseObject(String text, Class<T> clazz) {
a7d0f308   YunaiV   迁移 Redis 监控接口
41
42
43
          if (StrUtil.isEmpty(text)) {
              return null;
          }
eadc4f74   YunaiV   替换带代码里所有 fastjson...
44
45
46
47
48
          try {
              return objectMapper.readValue(text, clazz);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
c10ab175   YunaiV   优化 Redis pub/sub ...
49
50
51
      }
  
      public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
a7d0f308   YunaiV   迁移 Redis 监控接口
52
53
54
          if (ArrayUtil.isEmpty(bytes)) {
              return null;
          }
eadc4f74   YunaiV   替换带代码里所有 fastjson...
55
56
57
58
59
          try {
              return objectMapper.readValue(bytes, clazz);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
c10ab175   YunaiV   优化 Redis pub/sub ...
60
      }
ec8f181f   YunaiV   完成操作日志的记录
61
  
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
62
63
64
65
66
67
68
69
      public static Object parseObject(String text, TypeReference<Set<Long>> typeReference) {
          try {
              return objectMapper.readValue(text, typeReference);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
  
ec8f181f   YunaiV   完成操作日志的记录
70
  }