Blame view

src/main/java/cn/iocoder/dashboard/util/json/JsonUtils.java 2.25 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;
c91833a5   YunaiV   定义短信回调的结果
10
11
  import java.util.ArrayList;
  import java.util.List;
c10ab175   YunaiV   优化 Redis pub/sub ...
12
  
ec8f181f   YunaiV   完成操作日志的记录
13
14
15
16
17
  /**
   * JSON 工具类
   *
   * @author 芋道源码
   */
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
18
  public class JsonUtils {
ec8f181f   YunaiV   完成操作日志的记录
19
  
eadc4f74   YunaiV   替换带代码里所有 fastjson...
20
21
      private static ObjectMapper objectMapper = new ObjectMapper();
  
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
22
23
      /**
       * 初始化 objectMapper 属性
767cd902   zengzefeng   批量发送半成品
24
       * <p>
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
25
26
27
28
29
30
       * 通过这样的方式,使用 Spring 创建的 ObjectMapper Bean
       *
       * @param objectMapper ObjectMapper 对象
       */
      public static void init(ObjectMapper objectMapper) {
          JsonUtils.objectMapper = objectMapper;
eadc4f74   YunaiV   替换带代码里所有 fastjson...
31
32
      }
  
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
33
      public static String toJsonString(Object object) {
eadc4f74   YunaiV   替换带代码里所有 fastjson...
34
35
36
37
38
          try {
              return objectMapper.writeValueAsString(object);
          } catch (JsonProcessingException e) {
              throw new RuntimeException(e);
          }
c10ab175   YunaiV   优化 Redis pub/sub ...
39
40
41
      }
  
      public static <T> T parseObject(String text, Class<T> clazz) {
a7d0f308   YunaiV   迁移 Redis 监控接口
42
43
44
          if (StrUtil.isEmpty(text)) {
              return null;
          }
eadc4f74   YunaiV   替换带代码里所有 fastjson...
45
46
47
48
49
          try {
              return objectMapper.readValue(text, clazz);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
c10ab175   YunaiV   优化 Redis pub/sub ...
50
51
52
      }
  
      public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
a7d0f308   YunaiV   迁移 Redis 监控接口
53
54
55
          if (ArrayUtil.isEmpty(bytes)) {
              return null;
          }
eadc4f74   YunaiV   替换带代码里所有 fastjson...
56
57
58
59
60
          try {
              return objectMapper.readValue(bytes, clazz);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
c10ab175   YunaiV   优化 Redis pub/sub ...
61
      }
ec8f181f   YunaiV   完成操作日志的记录
62
  
c91833a5   YunaiV   定义短信回调的结果
63
      public static <T> T parseObject(String text, TypeReference<T> typeReference) {
fa3f3822   YunaiV   进一步优化 json 工具类,默认...
64
65
66
67
68
69
70
          try {
              return objectMapper.readValue(text, typeReference);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
  
c91833a5   YunaiV   定义短信回调的结果
71
72
73
74
      public static <T> List<T> parseArray(String text, Class<T> clazz) {
          if (StrUtil.isEmpty(text)) {
              return new ArrayList<>();
          }
767cd902   zengzefeng   批量发送半成品
75
          try {
c91833a5   YunaiV   定义短信回调的结果
76
              return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
767cd902   zengzefeng   批量发送半成品
77
78
79
80
81
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
  
ec8f181f   YunaiV   完成操作日志的记录
82
  }