疯狂的狮子li
2021-10-22 336adbd056818bae90b2bee57ae45ce8f1cd0524
ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java
@@ -1,10 +1,12 @@
package com.ruoyi.common.utils;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ArrayUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.io.IOException;
import java.util.ArrayList;
@@ -16,25 +18,15 @@
 *
 * @author 芋道源码
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JsonUtils {
    private static ObjectMapper objectMapper = new ObjectMapper();
    /**
     * 初始化 objectMapper 属性
     * <p>
     * 通过这样的方式,使用 Spring 创建的 ObjectMapper Bean
     *
     * @param objectMapper ObjectMapper 对象
     */
    public static void init(ObjectMapper objectMapper) {
        JsonUtils.objectMapper = objectMapper;
    }
    private static ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class);
    public static String toJsonString(Object object) {
      if (StringUtils.isNull(object)) {
         return null;
      }
        if (StringUtils.isNull(object)) {
            return null;
        }
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
@@ -65,9 +57,9 @@
    }
    public static <T> T parseObject(String text, TypeReference<T> typeReference) {
      if (StringUtils.isBlank(text)) {
         return null;
      }
        if (StringUtils.isBlank(text)) {
            return null;
        }
        try {
            return objectMapper.readValue(text, typeReference);
        } catch (IOException e) {
@@ -75,16 +67,17 @@
        }
    }
   public static <T> Map<String, T> parseMap(String text) {
      if (StringUtils.isBlank(text)) {
         return null;
      }
      try {
         return objectMapper.readValue(text, new TypeReference<Map<String, T>>() {});
      } catch (IOException e) {
         throw new RuntimeException(e);
      }
   }
    public static <T> Map<String, T> parseMap(String text) {
        if (StringUtils.isBlank(text)) {
            return null;
        }
        try {
            return objectMapper.readValue(text, new TypeReference<Map<String, T>>() {
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static <T> List<T> parseArray(String text, Class<T> clazz) {
        if (StringUtils.isEmpty(text)) {